From b311d7f8459fe1e90f7b0cbf5862b22a2cbedff5 Mon Sep 17 00:00:00 2001 From: sefidel Date: Tue, 23 Jan 2024 00:20:54 +0900 Subject: feat(lib)!: refactor --- lib/attrs.nix | 26 +++++++++++++++ lib/default.nix | 24 +++++++++----- lib/home_configurations_as_packages.nix | 12 ------- lib/mk_home.nix | 49 +++++++++++++++-------------- lib/mk_system.nix | 45 +++++++++++++++----------- lib/modules.nix | 54 ++++++++++++++++++++++++++++++++ lib/nixos_configurations_as_packages.nix | 12 ------- 7 files changed, 149 insertions(+), 73 deletions(-) create mode 100644 lib/attrs.nix delete mode 100644 lib/home_configurations_as_packages.nix create mode 100644 lib/modules.nix delete mode 100644 lib/nixos_configurations_as_packages.nix (limited to 'lib') diff --git a/lib/attrs.nix b/lib/attrs.nix new file mode 100644 index 0000000..0f8ebd1 --- /dev/null +++ b/lib/attrs.nix @@ -0,0 +1,26 @@ +{ lib, ... }: + +with builtins; +with lib; +rec { + # attrsToList + attrsToList = attrs: + mapAttrsToList (name: value: { inherit name value; }) attrs; + + # mapFilterAttrs :: + # (name -> value -> bool) + # (name -> value -> { name = any; value = any; }) + # attrs + mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs); + + # Generate an attribute set by mapping a function over a list of values. + genAttrs' = values: f: listToAttrs (map f values); + + # anyAttrs :: (name -> value -> bool) attrs + anyAttrs = pred: attrs: + any (attr: pred attr.name attr.value) (attrsToList attrs); + + # countAttrs :: (name -> value -> bool) attrs + countAttrs = pred: attrs: + count (attr: pred attr.name attr.value) (attrsToList attrs); +} diff --git a/lib/default.nix b/lib/default.nix index b75be10..8e95c8c 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,8 +1,18 @@ -inputs: +{ inputs, lib, ...}: -{ - mkSystem = import ./mk_system.nix inputs; - mkHome = import ./mk_home.nix inputs; - nixosConfigurationsAsPackages = import ./nixos_configurations_as_packages.nix inputs; - homeConfigurationsAsPackages = import ./home_configurations_as_packages.nix inputs; -} +let + inherit (lib) makeExtensible attrValues foldr; + inherit (modules) mapModules; + + modules = import ./modules.nix { + inherit lib; + self.attrs = import ./attrs.nix { inherit lib; self = { }; }; + }; + + mylib = makeExtensible (self: + mapModules ./. + (file: import file ({ inherit lib; } // inputs))); +in +mylib.extend + (self: super: + foldr (a: b: a//b) { } (attrValues super)) diff --git a/lib/home_configurations_as_packages.nix b/lib/home_configurations_as_packages.nix deleted file mode 100644 index 55d3e55..0000000 --- a/lib/home_configurations_as_packages.nix +++ /dev/null @@ -1,12 +0,0 @@ -{ self, ... } @ inputs: - -let - pkgs = import inputs.unstable { system = "x86_64-linux"; }; - - inherit (pkgs.lib) genAttrs mapAttrs'; - - hostNames = __attrNames self.homeConfigurations; - attrHostNames = genAttrs hostNames (name: "home/config/${name}"); - configs = mapAttrs' (name: pname: { name = pname; value = self.homeConfigurations.${name}.activationPackage; }) attrHostNames; -in -{ x86_64-linux = configs; } diff --git a/lib/mk_home.nix b/lib/mk_home.nix index 925fdbe..8e63324 100644 --- a/lib/mk_home.nix +++ b/lib/mk_home.nix @@ -1,29 +1,30 @@ { self, home-manager, ... } @ inputs: +{ + mkHome = { username, hostname, pkgs, version, extraModules ? null }: + let + entrypoint = "${self}/home/configs/${hostname}/${username}.nix"; + defaultModule = { lib, config, ... }: { + _module.args.inputs = self.inputs; + _module.args.self = self; -{ username, hostname, pkgs, version, extraModules ? null }: -let - entrypoint = "${self}/home/configs/${hostname}/${username}.nix"; - defaultModule = { lib, config, ... }: { - _module.args.inputs = self.inputs; - _module.args.self = self; + manual = { + html.enable = false; + manpages.enable = false; + json.enable = false; + }; - manual = { - html.enable = false; - manpages.enable = false; - json.enable = false; + home = { + inherit username; + homeDirectory = "/home/${username}"; + stateVersion = version; + }; + }; + in + home-manager.lib.homeManagerConfiguration { + pkgs = pkgs; + modules = [ + defaultModule + entrypoint + ] ++ pkgs.lib.optionals (extraModules != null) extraModules; }; - - home = { - inherit username; - homeDirectory = "/home/${username}"; - stateVersion = version; - }; - }; -in -home-manager.lib.homeManagerConfiguration { - pkgs = pkgs; - modules = [ - defaultModule - entrypoint - ] ++ pkgs.lib.optionals (extraModules != null) extraModules; } diff --git a/lib/mk_system.nix b/lib/mk_system.nix index 54dddbe..2aca550 100644 --- a/lib/mk_system.nix +++ b/lib/mk_system.nix @@ -1,23 +1,32 @@ -{ self, ... } @ args: - -{ name, nixpkgs, system ? "x86_64-linux", overlays ? null, extraModules ? null }: -nixpkgs.lib.nixosSystem ( - let - configFolder = "${self}/nixos"; - entryPoint = "${configFolder}/${name}/configuration.nix"; - hardware = "${configFolder}/${name}/hardware-configuration.nix"; - in - { - system = system; +{ self, unstable, ... } @ args: +{ + mkSystem = { name, nixpkgs ? unstable, system ? "x86_64-linux", overlays ? null, extraModules ? null }: + nixpkgs.lib.nixosSystem ( + let + configFolder = "${self}/nixos"; + entryPoint = "${configFolder}/${name}/configuration.nix"; + hardware = "${configFolder}/${name}/hardware-configuration.nix"; + in + { + system = system; - specialArgs = args; + specialArgs = args; - modules = [ - { - networking.hostName = name; - nix.flakes.enable = true; - system.configurationRevision = self.rev or "dirty"; - documentation.man = { enable = true; generateCaches = true; }; + modules = [ + { + networking.hostName = name; + nix.flakes.enable = true; + system.configurationRevision = self.rev or "dirty"; + documentation.man = { enable = true; generateCaches = true; }; + } + entryPoint + hardware + # TODO: import all modules (use mapModules?) + ../modules/flakes.nix + ../modules/nix.nix + ../modules/nixpkgs.nix + ] ++ nixpkgs.lib.optional (overlays != null) { nixpkgs.overlays = overlays; } + ++ nixpkgs.lib.optionals (extraModules != null) extraModules; } entryPoint hardware diff --git a/lib/modules.nix b/lib/modules.nix new file mode 100644 index 0000000..ef7c289 --- /dev/null +++ b/lib/modules.nix @@ -0,0 +1,54 @@ +{ self, lib, ... }: + +let + inherit (builtins) attrValues readDir pathExists concatLists; + inherit (lib) id mapAttrsToList filterAttrs hasPrefix hasSuffix nameValuePair removeSuffix; + inherit (self.attrs) mapFilterAttrs; +in +rec { + mapModules = dir: fn: + mapFilterAttrs + (n: v: + v != null && + !(hasPrefix "_" n)) + (n: v: + let path = "${toString dir}/${n}"; in + if v == "directory" && pathExists "${path}/default.nix" + then nameValuePair n (fn path) + else if v == "regular" && + n != "default.nix" && + hasSuffix ".nix" n + then nameValuePair (removeSuffix ".nix" n) (fn path) + else nameValuePair "" null) + (readDir dir); + + mapModules' = dir: fn: + attrValues (mapModules dir fn); + + mapModulesRec = dir: fn: + mapFilterAttrs + (n: v: + v != null && + !(hasPrefix "_" n)) + (n: v: + let path = "${toString dir}/${n}"; in + if v == "directory" + then nameValuePair n (mapModulesRec path fn) + else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n + then nameValuePair (removeSuffix ".nix" n) (fn path) + else nameValuePair "" null) + (readDir dir); + + mapModulesRec' = dir: fn: + let + dirs = + mapAttrsToList + (k: _: "${dir}/${k}") + (filterAttrs + (n: v: v == "directory" && !(hasPrefix "_" n)) + (readDir dir)); + files = attrValues (mapModules dir id); + paths = files ++ concatLists (map (d: mapModulesRec' d id) dirs); + in + map fn paths; +} diff --git a/lib/nixos_configurations_as_packages.nix b/lib/nixos_configurations_as_packages.nix deleted file mode 100644 index 356b09b..0000000 --- a/lib/nixos_configurations_as_packages.nix +++ /dev/null @@ -1,12 +0,0 @@ -{ self, ... } @ inputs: - -let - pkgs = import inputs.unstable { system = "x86_64-linux"; }; - - inherit (pkgs.lib) genAttrs mapAttrs'; - - hostNames = __attrNames self.nixosConfigurations; - attrHostNames = genAttrs hostNames (name: "nixos/config/${name}"); - configs = mapAttrs' (name: pname: { name = pname; value = self.nixosConfigurations.${name}.config.system.build.toplevel; }) attrHostNames; -in -{ x86_64-linux = configs; } -- cgit 1.4.1