summaryrefslogtreecommitdiff
path: root/flake.nix
diff options
context:
space:
mode:
Diffstat (limited to 'flake.nix')
-rw-r--r--flake.nix94
1 files changed, 94 insertions, 0 deletions
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 00000000..fa1e81d6
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,94 @@
1{
2 description = "gkleen's machines";
3
4 inputs = {
5 nixpkgs = {
6 type = "github";
7 owner = "NixOS";
8 repo = "nixpkgs";
9 ref = "master";
10 };
11 home-manager = {
12 type = "github";
13 owner = "nix-community";
14 repo = "home-manager";
15 ref = "master";
16 inputs.nixpkgs.follows = "nixpkgs";
17 };
18 };
19
20 outputs = { self, nixpkgs, home-manager }@inputs:
21 let
22 inherit (builtins) attrNames attrValues elemAt;
23 inherit (nixpkgs) lib;
24 utils = import ./utils { inherit lib; };
25 inherit (utils) recImport overrideModule;
26 inherit (lib) nixosSystem mkIf splitString filterAttrs listToAttrs mapAttrsToList nameValuePair concatMap composeManyExtensions mapAttrs mapAttrs' recursiveUpdate;
27
28 mkNixosConfiguration = dir: path: hostName: nixosSystem rec {
29 specialArgs = {
30 flake = self;
31 flakeInputs = inputs;
32 path = toString ./.;
33 };
34 modules =
35 let
36 defaultProfiles = with self.nixosModules.systemProfiles; [core];
37 local = "${toString dir}/${path}";
38 global._module.args = {
39 customUtils = utils;
40 inherit hostName;
41 };
42 accountModules = attrValues (filterAttrs accountMatchesHost self.nixosModules.accounts);
43 accountMatchesHost = n: _v:
44 let
45 accountName' = splitString "@" n;
46 hostName' = elemAt accountName' 1;
47 in hostName' == hostName;
48 in [ home-manager.nixosModules.home-manager global ] ++ defaultProfiles ++ [ local ] ++ accountModules;
49 };
50
51 mkSystemProfile = dir: path: profileName: {
52 imports = [ "${toString dir}/${path}" ];
53 config = {
54 system.profiles = [profileName];
55 };
56 };
57
58 mkUserModule = dir: path: userName: overrideModule (import "${toString dir}/${path}") (inputs: inputs // { inherit userName; }) (outputs: { _file = "${toString dir}/${path}"; } // outputs);
59
60 mkAccountModule = dir: path: accountName:
61 let
62 accountName' = splitString "@" accountName;
63 userName = elemAt accountName' 0;
64 in overrideModule (import "${toString dir}/${path}") (inputs: inputs // { inherit userName; }) (outputs: { _file = "${toString dir}/${path}"; } // outputs // { imports = [self.nixosModules.users.${userName}] ++ (outputs.imports or []); });
65
66 forAllSystems = f: mapAttrs f nixpkgs.legacyPackages;
67
68 activateHomeManagerConfigurations = forAllSystems (system: _pkgs: mapAttrs' (configName: hmConfig: nameValuePair "${configName}-activate" { type = "app"; program = "${hmConfig.activationPackage}/bin/activate"; }) self.homeManagerConfigurations);
69 activateNixosConfigurations = forAllSystems (system: _pkgs: mapAttrs' (hostName: nixosConfig: nameValuePair "${hostName}-activate" { type = "app"; program = "${nixosConfig.config.system.build.toplevel}/bin/switch-to-configuration"; }) self.nixosConfigurations);
70 in
71 {
72 nixosModules =
73 let modulesAttrs = recImport { dir = ./modules; };
74 systemProfiles = recImport rec { dir = ./system-profiles; _import = mkSystemProfile dir; };
75 userProfiles = recImport rec { dir = ./user-profiles; };
76 users = recImport rec { dir = ./users; _import = mkUserModule dir; };
77 accounts = recImport rec { dir = ./accounts; _import = mkAccountModule dir; };
78 in modulesAttrs // { inherit systemProfiles userProfiles users accounts; };
79 nixosConfigurations = recImport rec { dir = ./hosts; _import = mkNixosConfiguration dir; };
80
81 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)));
82
83 overlay = import ./pkgs;
84 overlays = recImport { dir = ./overlays; } // { pkgs = self.overlay; };
85
86 packages = forAllSystems (system: systemPkgs: composeManyExtensions (attrValues self.overlays) (self.legacyPackages.${system}) systemPkgs);
87
88 legacyPackages = forAllSystems (system: systemPkgs: recursiveUpdate systemPkgs self.packages.${system});
89
90 apps = recursiveUpdate activateNixosConfigurations activateHomeManagerConfigurations;
91
92 devShell = forAllSystems (system: systemPkgs: import ./shell.nix { pkgs = self.legacyPackages.${system}; });
93 };
94}