diff options
Diffstat (limited to 'flake.nix')
-rw-r--r-- | flake.nix | 77 |
1 files changed, 58 insertions, 19 deletions
@@ -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 | ||