diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2021-01-01 22:16:57 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2021-01-01 22:16:57 +0100 |
commit | 0ded132804541286e827dfda599c7a5ff898e07b (patch) | |
tree | 8db298842b4d8d1ca80f73d6ba80082ebe642ec0 | |
parent | a8c9742aae6f2672754c8c7f84694cf6a89d5a19 (diff) | |
download | nixos-0ded132804541286e827dfda599c7a5ff898e07b.tar nixos-0ded132804541286e827dfda599c7a5ff898e07b.tar.gz nixos-0ded132804541286e827dfda599c7a5ff898e07b.tar.bz2 nixos-0ded132804541286e827dfda599c7a5ff898e07b.tar.xz nixos-0ded132804541286e827dfda599c7a5ff898e07b.zip |
implement user profiles
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | flake.nix | 77 | ||||
-rw-r--r-- | system-profiles/core.nix | 8 | ||||
-rw-r--r-- | user-profiles/core.nix | 26 |
4 files changed, 90 insertions, 24 deletions
@@ -1,3 +1,4 @@ | |||
1 | **/result | 1 | **/result |
2 | **/result-* | 2 | **/result-* |
3 | **/#*# \ No newline at end of file | 3 | **/#*# |
4 | **/.#* \ No newline at end of file | ||
@@ -26,17 +26,26 @@ | |||
26 | 26 | ||
27 | outputs = { self, nixpkgs, home-manager, sops-nix }@inputs: | 27 | outputs = { self, nixpkgs, home-manager, sops-nix }@inputs: |
28 | let | 28 | let |
29 | inherit (builtins) attrNames attrValues elemAt; | 29 | inherit (builtins) attrNames attrValues elemAt toJSON; |
30 | inherit (nixpkgs) lib; | 30 | inherit (nixpkgs) lib; |
31 | utils = import ./utils { inherit lib; }; | 31 | utils = import ./utils { inherit lib; }; |
32 | inherit (utils) recImport overrideModule; | 32 | inherit (utils) recImport overrideModule; |
33 | inherit (lib) nixosSystem mkIf splitString filterAttrs listToAttrs mapAttrsToList nameValuePair concatMap composeManyExtensions mapAttrs mapAttrs' recursiveUpdate; | 33 | inherit (lib) nixosSystem mkIf splitString filterAttrs listToAttrs mapAttrsToList nameValuePair concatMap composeManyExtensions mapAttrs mapAttrs' recursiveUpdate genAttrs unique; |
34 | |||
35 | accountUserName = accountName: | ||
36 | let | ||
37 | accountName' = splitString "@" accountName; | ||
38 | in elemAt accountName' 0; | ||
39 | accountHostName = accountName: | ||
40 | let | ||
41 | accountName' = splitString "@" accountName; | ||
42 | in elemAt accountName' 1; | ||
34 | 43 | ||
35 | mkNixosConfiguration = dir: path: hostName: nixosSystem rec { | 44 | mkNixosConfiguration = dir: path: hostName: nixosSystem rec { |
36 | specialArgs = { | 45 | specialArgs = { |
37 | flake = self; | 46 | flake = self; |
38 | flakeInputs = inputs; | 47 | flakeInputs = inputs; |
39 | path = toString ./.; | 48 | path = ./.; |
40 | }; | 49 | }; |
41 | modules = | 50 | modules = |
42 | let | 51 | let |
@@ -44,54 +53,84 @@ | |||
44 | [ core | 53 | [ core |
45 | ]; | 54 | ]; |
46 | 55 | ||
47 | local = "${toString dir}/${path}"; | 56 | local = dir + "/${path}"; |
48 | argsModule._module.args = { | 57 | argsModule._module.args = { |
49 | customUtils = utils; | 58 | customUtils = utils; |
50 | inherit hostName; | 59 | inherit hostName; |
51 | }; | 60 | }; |
52 | accountModules = attrValues (filterAttrs accountMatchesHost self.nixosModules.accounts); | 61 | accountModules = attrValues (filterAttrs accountMatchesHost self.nixosModules.accounts); |
53 | accountMatchesHost = n: _v: | 62 | accountMatchesHost = n: _v: accountHostName n == hostName; |
54 | let | ||
55 | accountName' = splitString "@" n; | ||
56 | hostName' = elemAt accountName' 1; | ||
57 | in hostName' == hostName; | ||
58 | in [ argsModule ] ++ defaultProfiles ++ [ local ] ++ accountModules; | 63 | in [ argsModule ] ++ defaultProfiles ++ [ local ] ++ accountModules; |
59 | }; | 64 | }; |
60 | 65 | ||
61 | mkSystemProfile = dir: path: profileName: { | 66 | mkSystemProfile = dir: path: profileName: { |
62 | imports = [ "${toString dir}/${path}" ]; | 67 | imports = [ (dir + "/${path}") ]; |
63 | config = { | 68 | config = { |
64 | system.profiles = [profileName]; | 69 | system.profiles = [profileName]; |
65 | }; | 70 | }; |
66 | }; | 71 | }; |
67 | 72 | ||
68 | mkUserModule = dir: path: userName: overrideModule (import "${toString dir}/${path}") (inputs: inputs // { inherit userName; }) (outputs: { _file = "${toString dir}/${path}"; } // outputs); | 73 | defaultUserProfiles = userName: with self.nixosModules.userProfiles.${userName}; |
74 | [ core | ||
75 | ]; | ||
76 | |||
77 | mkUserModule = dir: path: userName: | ||
78 | overrideModule (import (dir + "/${path}")) | ||
79 | (inputs: inputs // { inherit userName; }) | ||
80 | (outputs: { _file = dir + "/${path}"; } | ||
81 | // outputs | ||
82 | // { imports = [self.nixosModules.userProfiles.${userName}.core] ++ (outputs.imports or []); }); | ||
83 | |||
84 | mkUserProfile = userName: dir: path: profileName: | ||
85 | let | ||
86 | profileModule = overrideModule (import (dir + "/${path}")) | ||
87 | (inputs: inputs // { inherit userName; }) | ||
88 | (outputs: { _file = dir + "/${path}"; } | ||
89 | // outputs); | ||
90 | in { | ||
91 | imports = [profileModule]; | ||
92 | config = { | ||
93 | users.users.${userName}.profiles = [profileName]; | ||
94 | }; | ||
95 | }; | ||
69 | 96 | ||
70 | mkAccountModule = dir: path: accountName: | 97 | mkAccountModule = dir: path: accountName: |
71 | let | 98 | let |
72 | accountName' = splitString "@" accountName; | 99 | userName = accountUserName accountName; |
73 | userName = elemAt accountName' 0; | 100 | in overrideModule |
74 | in overrideModule (import "${toString dir}/${path}") (inputs: inputs // { inherit userName; }) (outputs: { _file = "${toString dir}/${path}"; } // outputs // { imports = [self.nixosModules.users.${userName}] ++ (outputs.imports or []); }); | 101 | (import (dir + "/${path}")) |
102 | (inputs: inputs // { inherit userName; }) | ||
103 | (outputs: { _file = dir + "/${path}"; } | ||
104 | // outputs | ||
105 | // { imports = defaultUserProfiles userName ++ (outputs.imports or []); }); | ||
75 | 106 | ||
76 | forAllSystems = f: mapAttrs f nixpkgs.legacyPackages; | 107 | forAllSystems = f: mapAttrs f nixpkgs.legacyPackages; |
108 | forAllUsers = genAttrs (unique (map accountUserName (attrNames self.nixosModules.accounts))); | ||
77 | 109 | ||
78 | activateHomeManagerConfigurations = forAllSystems (system: _pkgs: mapAttrs' (configName: hmConfig: nameValuePair "${configName}-activate" { type = "app"; program = "${hmConfig.activationPackage}/bin/activate"; }) self.homeManagerConfigurations); | 110 | activateHomeManagerConfigurations = forAllSystems (system: _pkgs: mapAttrs' (configName: hmConfig: nameValuePair "${configName}-activate" { type = "app"; program = "${hmConfig.activationPackage}/bin/activate"; }) self.homeManagerConfigurations); |
79 | activateNixosConfigurations = forAllSystems (system: _pkgs: mapAttrs' (hostName: nixosConfig: nameValuePair "${hostName}-activate" { type = "app"; program = "${nixosConfig.config.system.build.toplevel}/bin/switch-to-configuration"; }) self.nixosConfigurations); | 111 | activateNixosConfigurations = forAllSystems (system: _pkgs: mapAttrs' (hostName: nixosConfig: nameValuePair "${hostName}-activate" { type = "app"; program = "${nixosConfig.config.system.build.toplevel}/bin/switch-to-configuration"; }) self.nixosConfigurations); |
112 | |||
113 | overlayPaths = recImport rec { dir = ./overlays; _import = (path: _name: dir + "/${path}"); } // { pkgs = ./pkgs; }; | ||
80 | in | 114 | in |
81 | { | 115 | { |
82 | nixosModules = | 116 | nixosModules = |
83 | let modulesAttrs = recImport { dir = ./modules; }; | 117 | let modulesAttrs = recImport { dir = ./modules; }; |
84 | systemProfiles = recImport rec { dir = ./system-profiles; _import = mkSystemProfile dir; }; | 118 | systemProfiles = recImport rec { dir = ./system-profiles; _import = mkSystemProfile dir; }; |
85 | userProfiles = recImport rec { dir = ./user-profiles; }; | ||
86 | users = recImport rec { dir = ./users; _import = mkUserModule dir; }; | 119 | users = recImport rec { dir = ./users; _import = mkUserModule dir; }; |
87 | accounts = recImport rec { dir = ./accounts; _import = mkAccountModule dir; }; | 120 | userProfiles = forAllUsers (userName: recImport rec { dir = ./user-profiles; _import = mkUserProfile userName dir; }); |
88 | in modulesAttrs // { inherit systemProfiles userProfiles users accounts; }; | 121 | accounts = recursiveUpdate rootAccounts (recImport rec { dir = ./accounts; _import = mkAccountModule dir; }); |
122 | rootAccounts = mapAttrs' (hostName: _value: nameValuePair "root@${hostName}" ({...}: { imports = [ self.nixosModules.users.root or ({...}: { imports = defaultUserProfiles "root"; }) ]; })) self.nixosConfigurations; | ||
123 | in modulesAttrs // { inherit systemProfiles users userProfiles accounts; }; | ||
89 | nixosConfigurations = recImport rec { dir = ./hosts; _import = mkNixosConfiguration dir; }; | 124 | nixosConfigurations = recImport rec { dir = ./hosts; _import = mkNixosConfiguration dir; }; |
90 | 125 | ||
126 | homeManagerModules = recImport rec { dir = ./user-profiles; }; | ||
91 | homeManagerConfigurations = listToAttrs (concatMap ({hostName, users}: mapAttrsToList (userName: homeConfig: nameValuePair "${userName}@${hostName}" homeConfig) users) (mapAttrsToList (hostName: nixosConfig: { inherit hostName; users = nixosConfig.config.home-manager.users; }) (self.nixosConfigurations))); | 127 | homeManagerConfigurations = listToAttrs (concatMap ({hostName, users}: mapAttrsToList (userName: homeConfig: nameValuePair "${userName}@${hostName}" homeConfig) users) (mapAttrsToList (hostName: nixosConfig: { inherit hostName; users = nixosConfig.config.home-manager.users; }) (self.nixosConfigurations))); |
92 | 128 | ||
93 | overlay = import ./pkgs; | 129 | overlay = import overlayPaths.pkgs; |
94 | overlays = recImport { dir = ./overlays; } // { pkgs = self.overlay; }; | 130 | overlays = mapAttrs (_name: path: import path) overlayPaths; |
131 | overlays-path = forAllSystems (system: _: self.legacyPackages.${system}.writeText "overlays.nix" '' | ||
132 | map import (builtins.attrValues (builtins.fromJSON ${self.legacyPackages.${system}.writeText "overlays.json" (toJSON overlayPaths)})); | ||
133 | ''); | ||
95 | 134 | ||
96 | packages = forAllSystems (system: systemPkgs: composeManyExtensions (attrValues self.overlays) (self.legacyPackages.${system}) systemPkgs); | 135 | packages = forAllSystems (system: systemPkgs: composeManyExtensions (attrValues self.overlays) (self.legacyPackages.${system}) systemPkgs); |
97 | 136 | ||
diff --git a/system-profiles/core.nix b/system-profiles/core.nix index f009c178..bd2004df 100644 --- a/system-profiles/core.nix +++ b/system-profiles/core.nix | |||
@@ -22,8 +22,7 @@ in { | |||
22 | networking.hostName = hostName; | 22 | networking.hostName = hostName; |
23 | system.configurationRevision = lib.mkIf (flake ? rev) flake.rev; | 23 | system.configurationRevision = lib.mkIf (flake ? rev) flake.rev; |
24 | 24 | ||
25 | nixpkgs.pkgs = flakeInputs.nixpkgs.legacyPackages.${config.nixpkgs.system}; | 25 | nixpkgs.pkgs = flake.legacyPackages.${config.nixpkgs.system}; |
26 | nixpkgs.overlays = lib.attrValues flake.overlays; | ||
27 | 26 | ||
28 | nix = { | 27 | nix = { |
29 | package = pkgs.nixUnstable; | 28 | package = pkgs.nixUnstable; |
@@ -34,12 +33,13 @@ in { | |||
34 | experimental-features = nix-command flakes ca-references | 33 | experimental-features = nix-command flakes ca-references |
35 | ''; | 34 | ''; |
36 | nixPath = [ | 35 | nixPath = [ |
37 | "nixpkgs=${path}" | 36 | "nixpkgs=${flakeInputs.nixpkgs.legacyPackages.${config.nixpkgs.system}.path}" |
37 | "nixpkgs-overlays=${flake.overlays-path.${config.nixpkgs.system}}" | ||
38 | ]; | 38 | ]; |
39 | registry = { | 39 | registry = { |
40 | nixpkgs.flake = flakeInputs.nixpkgs; | 40 | nixpkgs.flake = flakeInputs.nixpkgs; |
41 | home-manager.flake = flakeInputs.home-manager; | 41 | home-manager.flake = flakeInputs.home-manager; |
42 | machines.flake = flake; | 42 | nixos.flake = flake; |
43 | }; | 43 | }; |
44 | }; | 44 | }; |
45 | 45 | ||
diff --git a/user-profiles/core.nix b/user-profiles/core.nix new file mode 100644 index 00000000..6f473b1a --- /dev/null +++ b/user-profiles/core.nix | |||
@@ -0,0 +1,26 @@ | |||
1 | { flake, userName, lib, customUtils, ... }: | ||
2 | let | ||
3 | userProfileSet = customUtils.types.attrNameSet (lib.zipAttrs (lib.attrValues flake.nixosModules.userProfiles)); | ||
4 | in { | ||
5 | options = { | ||
6 | users.users = lib.mkOption { | ||
7 | type = lib.types.attrsOf (lib.types.submodule { | ||
8 | options.profiles = lib.mkOption { | ||
9 | type = userProfileSet; | ||
10 | default = []; | ||
11 | description = '' | ||
12 | Set (list without duplicates) of ‘userProfiles’ enabled for this user | ||
13 | ''; | ||
14 | }; | ||
15 | }); | ||
16 | }; | ||
17 | }; | ||
18 | |||
19 | config = { | ||
20 | users.users.${userName} = {}; # Just make sure the user is created | ||
21 | |||
22 | home-manager.users.${userName} = { | ||
23 | manual.manpages.enable = true; | ||
24 | }; | ||
25 | }; | ||
26 | } | ||