Partial way to unifying nixos and home-manager modules

This commit is contained in:
marc
2022-11-16 20:28:32 +01:00
parent 5c609ddd42
commit 882c04a6bf
8 changed files with 121 additions and 66 deletions

View File

@@ -29,17 +29,24 @@
reykjavik = lib.nixosSystem { reykjavik = lib.nixosSystem {
inherit system; inherit system;
inherit pkgs; inherit pkgs;
specialArgs = { inherit inputs; }; specialArgs = { inherit lib inputs system; };
modules = [ modules = [
./system/modules ./system/modules
./system/profiles ./system/profiles
./system/hosts/reykjavik ./system/hosts/reykjavik
home-manager.nixosModules.home-manager { {
home-manager.extraSpecialArgs = { inherit inputs; }; imports =
home-manager.useGlobalPkgs = true; [ inputs.home-manager.nixosModules.home-manager ]
home-manager.useUserPackages = true; # All my personal modules
++ (lib.my.mapModulesRec' (toString ./modules) import);
}
{
# home-manager.extraSpecialArgs = { inherit inputs; };
# home-manager.useGlobalPkgs = true;
# home-manager.useUserPackages = true;
home-manager.users.marc = { home-manager.users.marc = {
imports = [ imports = [
./home/modules ./home/modules
@@ -53,29 +60,29 @@
}; };
# - Kopavogur ---------------------------- # - Kopavogur ----------------------------
kopavogur = lib.nixosSystem { # kopavogur = lib.nixosSystem {
inherit system; # inherit system;
inherit pkgs; # inherit pkgs;
modules = [ # modules = [
./system/modules # ./system/modules
./system/profiles # ./system/profiles
./system/hosts/kopavogur # ./system/hosts/kopavogur
home-manager.nixosModules.home-manager { # home-manager.nixosModules.home-manager {
home-manager.useGlobalPkgs = true; # home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true; # home-manager.useUserPackages = true;
home-manager.users.marc = { # home-manager.users.marc = {
imports = [ # imports = [
./home/modules # ./home/modules
./home/profiles # ./home/profiles
./home/hosts/kopavogur.nix # ./home/hosts/kopavogur.nix
]; # ];
}; # };
} # }
]; # ];
}; # };
}; };
}; };

View File

@@ -11,6 +11,8 @@ rec {
# (name -> value -> bool) # (name -> value -> bool)
# (name -> value -> { name = any; value = any; }) # (name -> value -> { name = any; value = any; })
# attrs # attrs
# Generate an attribute set by mapping a function (f) to it and then filter
# by the predicate (pred)
mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs); mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs);
# Generate an attribute set by mapping a function over a list of values. # Generate an attribute set by mapping a function over a list of values.

View File

@@ -6,6 +6,10 @@ let
inherit (self.attrs) mapFilterAttrs; inherit (self.attrs) mapFilterAttrs;
in in
rec { rec {
# Returns an attribute set with the keys being the files in a directory
# and the values a function applied to their paths
# Only *.nix files and directories with a default.nix file are considered.
# The ".nix" suffix is removed from the keys of the attribute set.
mapModules = dir: fn: mapModules = dir: fn:
mapFilterAttrs mapFilterAttrs
(n: v: (n: v:
@@ -22,9 +26,14 @@ rec {
else nameValuePair "" null) else nameValuePair "" null)
(readDir dir); (readDir dir);
# Like mapModules above, but it just returns the values
# (map the function fn over the files in a directory)
mapModules' = dir: fn: mapModules' = dir: fn:
attrValues (mapModules dir fn); attrValues (mapModules dir fn);
# Like mapModules, but recursive. That is, if there is a directory, the
# value will be another attribute set with its contents as keys and the funcion
# applied to them as values.
mapModulesRec = dir: fn: mapModulesRec = dir: fn:
mapFilterAttrs mapFilterAttrs
(n: v: (n: v:
@@ -39,6 +48,9 @@ rec {
else nameValuePair "" null) else nameValuePair "" null)
(readDir dir); (readDir dir);
# Like mapModulesRec but flattened and only the values. That is, it returns a list
# with fn applied to all *.nix files within the directory and subdirectories
# excluding default.nix files.
mapModulesRec' = dir: fn: mapModulesRec' = dir: fn:
let let
dirs = dirs =

71
modules/options.nix Normal file
View File

@@ -0,0 +1,71 @@
{ config, options, lib, home-manager, ... }:
with lib;
with lib.my;
{
options = with types; {
user = mkOpt attrs {};
home = {
file = mkOpt' attrs {} "Files to place directly in $HOME";
configFile = mkOpt' attrs {} "Files to place in $XDG_CONFIG_HOME";
dataFile = mkOpt' attrs {} "Files to place in $XDG_DATA_HOME";
};
env = mkOption {
# env = { PATH = ["$PATH" ./test]; TEST = "test"; }
type = attrsOf (oneOf [ str path (listOf (either str path)) ]);
# env = { PATH = "$PATH:./test"; TEST = "test"; }
apply = mapAttrs
(n: v: if isList v
then concatMapStringsSep ":" (x: toString x) v
else (toString v));
default = {};
description = "Set environment variables that will be joined by a colon";
};
};
config = {
user = let name = "marc"; in {
inherit name;
description = "The primary user account";
extraGroups = [ "wheel" ];
isNormalUser = true;
home = "/home/${name}";
group = "users";
uid = 1000;
};
home-manager = {
# extraSpecialArgs = { inherit inputs; };
useGlobalPkgs = true;
useUserPackages = true;
users.${config.user.name} = {
home = {
file = mkAliasDefinitions options.home.file;
# Necessary for home-manager to work with flakes, otherwise it will
# look for a nixpkgs channel.
stateVersion = config.system.stateVersion;
};
xdg = {
configFile = mkAliasDefinitions options.home.configFile;
dataFile = mkAliasDefinitions options.home.dataFile;
};
};
};
users.users.${config.user.name} = mkAliasDefinitions options.user;
nix.settings = let users = [ "root" config.user.name ]; in {
trusted-users = users;
allowed-users = users;
};
env.PATH = [ "$PATH" ];
environment.extraInit =
concatStringsSep "\n"
(mapAttrsToList (n: v: "export ${n}=\"${v}\"") config.env);
};
}

View File

@@ -9,9 +9,9 @@
networking.hostName = "kopavogur"; networking.hostName = "kopavogur";
samfelag.modules.user = { # samfelag.modules.user = {
name = "marc"; # name = "marc";
}; # };
# - Bootloader --------------------------------- # - Bootloader ---------------------------------

View File

@@ -15,7 +15,7 @@
# - Modules ------------------------------------ # - Modules ------------------------------------
samfelag.modules = { samfelag.modules = {
user.name = "marc"; # user.name = "marc";
bluetooth.enable = true; bluetooth.enable = true;
editors.emacs.enable = true; editors.emacs.enable = true;
}; };

View File

@@ -5,6 +5,5 @@
./editors ./editors
./bluetooth.nix ./bluetooth.nix
./tailscale.nix ./tailscale.nix
./user.nix
]; ];
} }

View File

@@ -1,36 +0,0 @@
{ config, lib, pkgs, self, ... }:
let
cfg = config.samfelag.modules.user;
in
{
options.samfelag.modules.user = {
name = lib.mkOption {
type = lib.types.str;
default = "marc";
description = ''
Specifies the user name
'';
};
};
config = lib.mkMerge [
{
programs.zsh.enable = true;
users = {
defaultUserShell = pkgs.zsh;
users."${cfg.name}" = with cfg; {
description = "Marc Sastre Rienitz";
isNormalUser = true;
extraGroups = [ "networkmanager" "wheel" ];
};
# Do not allow users to be added or modified except through Nix configuration.
# mutableUsers = false;
};
nix.settings.trusted-users = [ "${cfg.name}" ];
}
];
}