From dd2391d9051a8d0d9cc60aa2805d8ce523d74e77 Mon Sep 17 00:00:00 2001 From: reo101 Date: Sun, 27 Nov 2022 02:12:21 +0200 Subject: [PATCH] feat!(flake): convert to fs-defined configurations Place all configurations in `machines/${manager}/${system}/${hostname}/...`: - `${manager}` - One of `nixos`, `nix-on-droid`, `nix-darwin` or `home-manager` - `${system}` - A system's architecture (can be many) - `${hostname}` - A system's hostname (can be many) The flake now automatically generates the needed configurations based on the above structure. It only generates configurations for valid directory structures: - It wouldn't generate a NixOS config if there isn't a `configuration.nix` file - ... --- flake.nix | 206 ++++++++++++------ lib/default.nix | 36 +++ .../apavel-a01/configuration.nix | 0 .../x86_64-darwin}/apavel-a01/home/apavel.nix | 0 .../aarch64-linux}/cheetah/configuration.nix | 0 .../aarch64-linux}/cheetah/home.nix | 0 .../home-manager}/djam/home.nix | 0 .../nixos}/arthur/configuration.nix | 0 .../nixos}/arthur/hardware-configuration.nix | 0 9 files changed, 176 insertions(+), 66 deletions(-) create mode 100644 lib/default.nix rename {nix-darwin => machines/nix-darwin/x86_64-darwin}/apavel-a01/configuration.nix (100%) rename {nix-darwin => machines/nix-darwin/x86_64-darwin}/apavel-a01/home/apavel.nix (100%) rename {nix-on-droid => machines/nix-on-droid/aarch64-linux}/cheetah/configuration.nix (100%) rename {nix-on-droid => machines/nix-on-droid/aarch64-linux}/cheetah/home.nix (100%) rename {home-manager => templates/home-manager}/djam/home.nix (100%) rename {nixos => templates/nixos}/arthur/configuration.nix (100%) rename {nixos => templates/nixos}/arthur/hardware-configuration.nix (100%) diff --git a/flake.nix b/flake.nix index d2e72ee..c386159 100644 --- a/flake.nix +++ b/flake.nix @@ -67,6 +67,10 @@ } @ inputs: let inherit (self) outputs; + inherit ((import ./lib/default.nix) { lib = nixpkgs.lib; }) + recurseDir + hasFiles + hasDirectories; forAllSystems = nixpkgs.lib.genAttrs [ "aarch64-linux" "i686-linux" @@ -87,82 +91,152 @@ ); overlays = import ./overlays; + + # Modules nixosModules = import ./modules/nixos; nixOnDroidModules = import ./modules/nix-on-droid; nixDarwinModules = import ./modules/nix-darwin; homeManagerModules = import ./modules/home-manager; - nixosConfigurations = { - # arthur = nixpkgs.lib.nixosSystem { - # specialArgs = { inherit inputs outputs; }; - # modules = [ - # ./nixos/arthur/configuration.nix - # ]; - # }; + # Machines + machines = recurseDir ./machines; + homeManagerMachines = machines.home-manager or {}; + nixDarwinMachines = machines.nix-darwin or {}; + nixOnDroidMachines = machines.nix-on-droid or {}; + nixosMachines = machines.nixos or {}; + + # mkHost helpers + mkNixosHost = system: hostname: nixpkgs.lib.nixosSystem { + modules = [ + ./machines/nixos/${system}/${hostname}/configuration.nix + ]; + + specialArgs = { + inherit inputs outputs; + }; }; + mkNixOnDroidHost = system: hostname: nix-on-droid.lib.nixOnDroidConfiguration { + pkgs = import nixpkgs { + inherit system; + + overlays = [ + nix-on-droid.overlays.default + ]; + }; + + modules = [ + ./machines/nix-on-droid/${system}/${hostname}/configuration.nix + { nix.registry.nixpkgs.flake = nixpkgs; } + ] ++ (builtins.attrValues nixOnDroidModules); + + extraSpecialArgs = { + inherit inputs outputs; + # rootPath = ./.; + }; + + home-manager-path = home-manager.outPath; + }; + + mkNixDarwinHost = system: hostname: users: nix-darwin.lib.darwinSystem { + inherit system; + modules = [ + ./machines/nix-darwin/${system}/${hostname}/configuration.nix + home-manager.darwinModules.home-manager + { + home-manager = { + useGlobalPkgs = false; + useUserPackages = true; + users = nixpkgs.lib.attrsets.genAttrs + users + (user: import ./machines/nix-darwin/${system}/${hostname}/home/${user}.nix); + + extraSpecialArgs = { inherit inputs; }; + }; + } + ] ++ (builtins.attrValues nixDarwinModules); + inputs = { inherit inputs outputs nix-darwin nixpkgs; }; + }; + + mkHomeManagerHost = system: hostname: home-manager.lib.homeManagerConfiguration { + pkgs = nixpkgs.legacyPackages.${system}; + modules = [ + ./machines/home-manager/${system}/${hostname}/home.nix + ]; + extraSpecialArgs = { inherit inputs outputs; }; + }; + + # Final configurations + + nixosConfigurations = + nixpkgs.lib.foldAttrs + (acc: x: acc) + [] + (builtins.attrValues + (builtins.mapAttrs + (system: hosts: + nixpkgs.lib.attrsets.filterAttrs + (host: config: config != null) + (builtins.mapAttrs + (host: config: + if (hasFiles [ "configuration.nix" ] config) + then mkNixosHost system host + else null) + hosts)) + nixosMachines)); + nixOnDroidConfigurations = - let mkHost = system: hostname: nix-on-droid.lib.nixOnDroidConfiguration { - pkgs = import nixpkgs { - inherit system; - - overlays = [ - nix-on-droid.overlays.default - ]; - }; - - modules = [ - ./nix-on-droid/${hostname}/configuration.nix - { nix.registry.nixpkgs.flake = nixpkgs; } - ] ++ (builtins.attrValues nixOnDroidModules); - - extraSpecialArgs = { - inherit inputs outputs; - # rootPath = ./.; - }; - - home-manager-path = home-manager.outPath; - }; - in - rec { - cheetah = mkHost "aarch64-linux" "cheetah"; - - default = cheetah; - }; + nixpkgs.lib.foldAttrs + (acc: x: acc) + [] + (builtins.attrValues + (builtins.mapAttrs + (system: hosts: + nixpkgs.lib.attrsets.filterAttrs + (host: config: config != null) + (builtins.mapAttrs + (host: config: + if (hasFiles [ "configuration.nix" "home.nix" ] config) + then mkNixOnDroidHost system host + else null) + hosts)) + nixOnDroidMachines)); darwinConfigurations = - let mkHost = system: hostname: users: nix-darwin.lib.darwinSystem { - inherit system; - modules = [ - ./nix-darwin/${hostname}/configuration.nix - home-manager.darwinModules.home-manager - { - home-manager = { - useGlobalPkgs = false; - useUserPackages = true; - users = nixpkgs.lib.attrsets.genAttrs - users - (user: import ./nix-darwin/${hostname}/home/${user}.nix); + nixpkgs.lib.foldAttrs + (acc: x: acc) + [] + (builtins.attrValues + (builtins.mapAttrs + (system: hosts: + nixpkgs.lib.attrsets.filterAttrs + (host: config: config != null) + (builtins.mapAttrs + (host: config: + if (hasFiles [ "configuration.nix" ] config) + then mkNixDarwinHost system host + (builtins.map + (nixpkgs.lib.strings.removeSuffix ".nix") + (builtins.attrNames (config."home" or {}))) + else null) + hosts)) + nixDarwinMachines)); - extraSpecialArgs = { inherit inputs; }; - }; - } - ] ++ (builtins.attrValues nixDarwinModules); - inputs = { inherit inputs outputs nix-darwin nixpkgs; }; - }; - in - rec { - apavel-a01 = mkHost "x86_64-darwin" "apavel-a01" [ "apavel" ]; - }; - - homeConfigurations = { - # "nix-on-droid@cheetah" = home-manager.lib.homeManagerConfiguration { - # pkgs = nixpkgs.legacyPackages.x86_64-linux; - # extraSpecialArgs = { inherit inputs outputs; }; - # modules = [ - # ./home-manager/home.nix - # ]; - # }; - }; + homeConfigurations = + nixpkgs.lib.foldAttrs + (acc: x: acc) + [] + (builtins.attrValues + (builtins.mapAttrs + (system: hosts: + nixpkgs.lib.attrsets.filterAttrs + (host: config: config != null) + (builtins.mapAttrs + (host: config: + if (hasFiles [ "home.nix" ] config) + then mkNixOnDroidHost system host + else null) + hosts)) + homeManagerMachines)); }; } diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..f420c4b --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,36 @@ +{ lib, ... }: + +let + inherit (lib) mapAttrs; + inherit (lib.attrsets) filterAttrs; +in +rec { + recurseDir = dir: + mapAttrs + (file: type: + if type == "directory" + then recurseDir "${dir}/${file}" + else type + ) + (builtins.readDir dir); + + # VVV - Implying `attrs` is the output of `recurseDir` - VVV + + hasFiles = files: attrs: + builtins.all + (b: b) + (builtins.map + (file: + builtins.hasAttr file attrs && + builtins.getAttr file attrs == "regular") + files); + + hasDirectories = directories: attrs: + builtins.all + (b: b) + (builtins.map + (directory: + builtins.hasAttr directory attrs && + builtins.getAttr directory attrs == "set") + directories); +} diff --git a/nix-darwin/apavel-a01/configuration.nix b/machines/nix-darwin/x86_64-darwin/apavel-a01/configuration.nix similarity index 100% rename from nix-darwin/apavel-a01/configuration.nix rename to machines/nix-darwin/x86_64-darwin/apavel-a01/configuration.nix diff --git a/nix-darwin/apavel-a01/home/apavel.nix b/machines/nix-darwin/x86_64-darwin/apavel-a01/home/apavel.nix similarity index 100% rename from nix-darwin/apavel-a01/home/apavel.nix rename to machines/nix-darwin/x86_64-darwin/apavel-a01/home/apavel.nix diff --git a/nix-on-droid/cheetah/configuration.nix b/machines/nix-on-droid/aarch64-linux/cheetah/configuration.nix similarity index 100% rename from nix-on-droid/cheetah/configuration.nix rename to machines/nix-on-droid/aarch64-linux/cheetah/configuration.nix diff --git a/nix-on-droid/cheetah/home.nix b/machines/nix-on-droid/aarch64-linux/cheetah/home.nix similarity index 100% rename from nix-on-droid/cheetah/home.nix rename to machines/nix-on-droid/aarch64-linux/cheetah/home.nix diff --git a/home-manager/djam/home.nix b/templates/home-manager/djam/home.nix similarity index 100% rename from home-manager/djam/home.nix rename to templates/home-manager/djam/home.nix diff --git a/nixos/arthur/configuration.nix b/templates/nixos/arthur/configuration.nix similarity index 100% rename from nixos/arthur/configuration.nix rename to templates/nixos/arthur/configuration.nix diff --git a/nixos/arthur/hardware-configuration.nix b/templates/nixos/arthur/hardware-configuration.nix similarity index 100% rename from nixos/arthur/hardware-configuration.nix rename to templates/nixos/arthur/hardware-configuration.nix