From 5cf9fcd62ec734b70f68c414658e560ea76c7682 Mon Sep 17 00:00:00 2001 From: reo101 Date: Tue, 14 Feb 2023 11:10:08 +0200 Subject: [PATCH] feat(flake): clean up Move helpers to `./helpers.nix` --- flake.nix | 192 ++++------------------------------------- helpers.nix | 223 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/helpers.nix | 36 -------- 3 files changed, 240 insertions(+), 211 deletions(-) create mode 100644 helpers.nix delete mode 100644 lib/helpers.nix diff --git a/flake.nix b/flake.nix index d68e95d..1b75699 100644 --- a/flake.nix +++ b/flake.nix @@ -75,18 +75,9 @@ let inherit (self) outputs; inherit (nixpkgs) lib; - helpers = (import ./lib/helpers.nix) { inherit lib; }; - inherit (helpers) recurseDir hasFiles hasDirectories; - forEachSystem = lib.genAttrs [ - "aarch64-linux" - "i686-linux" - "x86_64-linux" - "aarch64-darwin" - "x86_64-darwin" - ]; - forEachPkgs = f: forEachSystem (system': f nixpkgs.legacyPackages.${system'}); + helpers = import ./helpers.nix { inherit inputs outputs lib; }; in - rec { + with helpers; rec { # Packages (`nix build`) packages = forEachPkgs (pkgs: import ./pkgs { inherit pkgs; } @@ -111,172 +102,23 @@ # Overlays overlays = import ./overlays { inherit inputs outputs; }; - # Modules - nixosModules = import ./modules/nixos; - nixOnDroidModules = import ./modules/nix-on-droid; - nixDarwinModules = import ./modules/nix-darwin; - homeManagerModules = import ./modules/home-manager; - # Machines - machines = recurseDir ./machines; - homeManagerMachines = machines.home-manager or { }; - nixDarwinMachines = machines.nix-darwin or { }; - nixOnDroidMachines = machines.nix-on-droid or { }; - nixosMachines = machines.nixos or { }; + inherit machines; + inherit homeManagerMachines; + inherit nixDarwinMachines; + inherit nixOnDroidMachines; + inherit nixosMachines; - # mkHost helpers - mkNixosHost = system: hostname: users: lib.nixosSystem { - inherit system; + # Modules + inherit nixosModules; + inherit nixOnDroidModules; + inherit nixDarwinModules; + inherit homeManagerModules; - modules = [ - ./machines/nixos/${system}/${hostname}/configuration.nix - home-manager.nixosModules.home-manager - { - home-manager = { - useGlobalPkgs = false; - useUserPackages = true; - users = lib.attrsets.genAttrs - users - (user: import ./machines/nixos/${system}/${hostname}/home/${user}.nix); - - extraSpecialArgs = { - inherit inputs outputs; - }; - }; - } - ] ++ (builtins.attrValues nixosModules); - - 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 = lib.attrsets.genAttrs - users - (user: import ./machines/nix-darwin/${system}/${hostname}/home/${user}.nix); - - extraSpecialArgs = { inherit inputs outputs; }; - }; - } - ] ++ (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 - ] ++ (builtins.attrValues homeManagerModules); - - extraSpecialArgs = { inherit inputs outputs; }; - }; - - - createConfigurations = - pred: mkHost: machines: - lib.foldAttrs - (acc: x: acc) - [ ] - (builtins.attrValues - (builtins.mapAttrs - (system: hosts: - lib.attrsets.filterAttrs - (host: config: config != null) - (builtins.mapAttrs - (host: config: - if (pred system host config) - then mkHost system host config - else null) - hosts)) - machines)); - - # Final configurations - nixosConfigurations = - createConfigurations - (system: host: config: - hasFiles - [ "configuration.nix" ] - config) - (system: host: config: - mkNixosHost - system - host - (builtins.map - (lib.strings.removeSuffix ".nix") - (builtins.attrNames (config."home" or { })))) - nixosMachines; - - nixOnDroidConfigurations = - createConfigurations - (system: host: config: - hasFiles - [ "configuration.nix" "home.nix" ] - config) - (system: host: config: - mkNixOnDroidHost - system - host) - nixOnDroidMachines; - - darwinConfigurations = - createConfigurations - (system: host: config: - hasFiles - [ "configuration.nix" ] - config) - (system: host: config: - mkNixDarwinHost - system - host - (builtins.map - (lib.strings.removeSuffix ".nix") - (builtins.attrNames (config."home" or { })))) - nixDarwinMachines; - - homeConfigurations = - createConfigurations - (system: host: config: - hasFiles - [ "home.nix" ] - config) - (system: host: config: - mkHomeManagerHost - system - host) - homeManagerMachines; + # Configurations + nixosConfigurations = autoNixosConfigurations; + nixOnDroidConfigurations = autoNixOnDroidConfigurations; + darwinConfigurations = autoDarwinConfigurations; + homeConfigurations = autoHomeConfigurations; }; } diff --git a/helpers.nix b/helpers.nix new file mode 100644 index 0000000..d0705ba --- /dev/null +++ b/helpers.nix @@ -0,0 +1,223 @@ +{ inputs, outputs, lib, ... }: + +let + inherit (inputs) nixpkgs; + inherit (lib) mapAttrs; + inherit (lib.attrsets) filterAttrs; +in +rec { + # Directory walker + recurseDir = dir: + mapAttrs + (file: type: + if type == "directory" + then recurseDir "${dir}/${file}" + else type + ) + (builtins.readDir dir); + + # NOTE: Implying `attrs` is the output of `recurseDir` + hasFiles = files: attrs: + builtins.all + (b: b) + (builtins.map + (file: + builtins.hasAttr file attrs && + builtins.getAttr file attrs == "regular") + files); + + # NOTE: Implying `attrs` is the output of `recurseDir` + hasDirectories = directories: attrs: + builtins.all + (b: b) + (builtins.map + (directory: + builtins.hasAttr directory attrs && + builtins.getAttr directory attrs == "set") + directories); + + # pkgs helpers + forEachSystem = lib.genAttrs [ + "aarch64-linux" + "i686-linux" + "x86_64-linux" + "aarch64-darwin" + "x86_64-darwin" + ]; + + forEachPkgs = f: + forEachSystem + (system: + f nixpkgs.legacyPackages.${system}); + + # Modules + nixosModules = import ./modules/nixos; + nixOnDroidModules = import ./modules/nix-on-droid; + nixDarwinModules = import ./modules/nix-darwin; + homeManagerModules = import ./modules/home-manager; + + # Machines + machines = recurseDir ./machines; + homeManagerMachines = machines.home-manager or { }; + nixDarwinMachines = machines.nix-darwin or { }; + nixOnDroidMachines = machines.nix-on-droid or { }; + nixosMachines = machines.nixos or { }; + + # Configuration helpers + mkNixosHost = root: system: hostname: users: lib.nixosSystem { + inherit system; + + modules = [ + (root + "/configuration.nix") + inputs.home-manager.nixosModules.home-manager + { + home-manager = { + useGlobalPkgs = false; + useUserPackages = true; + users = lib.attrsets.genAttrs + users + (user: import (root + "/home/${user}.nix")); + + extraSpecialArgs = { + inherit inputs outputs; + }; + }; + } + ] ++ (builtins.attrValues nixosModules); + + specialArgs = { + inherit inputs outputs; + }; + }; + + mkNixOnDroidHost = root: system: hostname: inputs.nix-on-droid.lib.nixOnDroidConfiguration { + pkgs = import nixpkgs { + inherit system; + + overlays = [ + inputs.nix-on-droid.overlays.default + ]; + }; + + modules = [ + (root + "/configuration.nix") + { nix.registry.nixpkgs.flake = nixpkgs; } + ] ++ (builtins.attrValues nixOnDroidModules); + + extraSpecialArgs = { + inherit inputs outputs; + # rootPath = ./.; + }; + + home-manager-path = inputs.home-manager.outPath; + }; + + mkNixDarwinHost = root: system: hostname: users: inputs.nix-darwin.lib.darwinSystem { + inherit system; + + modules = [ + (root + "/configuration.nix") + inputs.home-manager.darwinModules.home-manager + { + home-manager = { + useGlobalPkgs = false; + useUserPackages = true; + users = lib.attrsets.genAttrs + users + (user: import (root + "/home/${user}.nix")); + + extraSpecialArgs = { inherit inputs outputs; }; + }; + } + ] ++ (builtins.attrValues nixDarwinModules); + + inputs = { inherit inputs outputs nixpkgs; }; + }; + + mkHomeManagerHost = root: system: hostname: inputs.home-manager.lib.homeManagerConfiguration { + pkgs = nixpkgs.legacyPackages.${system}; + + modules = [ + (root + "/home.nix") + ] ++ (builtins.attrValues homeManagerModules); + + extraSpecialArgs = { inherit inputs outputs; }; + }; + + createConfigurations = + pred: mkHost: machines: + lib.foldAttrs + (acc: x: acc) + [ ] + (builtins.attrValues + (builtins.mapAttrs + (system: hosts: + lib.attrsets.filterAttrs + (host: config: config != null) + (builtins.mapAttrs + (host: config: + if (pred system host config) + then mkHost system host config + else null) + hosts)) + machines)); + + # Configurations + autoNixosConfigurations = + createConfigurations + (system: host: config: + hasFiles + [ "configuration.nix" ] + config) + (system: host: config: + mkNixosHost + ./machines/nixos/${system}/${host} + system + host + (builtins.map + (lib.strings.removeSuffix ".nix") + (builtins.attrNames (config."home" or { })))) + nixosMachines; + + autoNixOnDroidConfigurations = + createConfigurations + (system: host: config: + hasFiles + [ "configuration.nix" "home.nix" ] + config) + (system: host: config: + mkNixOnDroidHost + ./machines/nix-on-droid/${system}/${host} + system + host) + nixOnDroidMachines; + + autoDarwinConfigurations = + createConfigurations + (system: host: config: + hasFiles + [ "configuration.nix" ] + config) + (system: host: config: + mkNixDarwinHost + ./machines/nix-darwin/${system}/${host} + system + host + (builtins.map + (lib.strings.removeSuffix ".nix") + (builtins.attrNames (config."home" or { })))) + nixDarwinMachines; + + autoHomeConfigurations = + createConfigurations + (system: host: config: + hasFiles + [ "home.nix" ] + config) + (system: host: config: + mkHomeManagerHost + ./machines/home-manager/${system}/${host} + system + host) + homeManagerMachines; +} diff --git a/lib/helpers.nix b/lib/helpers.nix deleted file mode 100644 index 8defe5b..0000000 --- a/lib/helpers.nix +++ /dev/null @@ -1,36 +0,0 @@ -{ 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); -}