diff options
author | sefidel <contact@sefidel.net> | 2024-01-23 00:20:54 +0900 |
---|---|---|
committer | sefidel <contact@sefidel.net> | 2024-01-23 01:05:04 +0900 |
commit | b311d7f8459fe1e90f7b0cbf5862b22a2cbedff5 (patch) | |
tree | d7b8af8e685699a1e66afcb10b668363d604106e | |
parent | b670811db1c645c9c4effff1829e90b380318427 (diff) | |
download | nixrc-b311d7f8459fe1e90f7b0cbf5862b22a2cbedff5.tar.gz nixrc-b311d7f8459fe1e90f7b0cbf5862b22a2cbedff5.zip |
feat(lib)!: refactor
-rw-r--r-- | flake.nix | 10 | ||||
-rw-r--r-- | lib/attrs.nix | 26 | ||||
-rw-r--r-- | lib/default.nix | 24 | ||||
-rw-r--r-- | lib/home_configurations_as_packages.nix | 12 | ||||
-rw-r--r-- | lib/mk_home.nix | 49 | ||||
-rw-r--r-- | lib/mk_system.nix | 45 | ||||
-rw-r--r-- | lib/modules.nix | 54 | ||||
-rw-r--r-- | lib/nixos_configurations_as_packages.nix | 12 |
8 files changed, 155 insertions, 77 deletions
diff --git a/flake.nix b/flake.nix index 48147e6..9cb3aa9 100644 --- a/flake.nix +++ b/flake.nix @@ -31,7 +31,11 @@ nix-gaming.inputs.nixpkgs.follows = "unstable"; }; - outputs = { self, ... } @ inputs: + outputs = { self, unstable, ... } @ inputs: + let + lib = unstable.lib.extend + (self: super: { my = import ./lib { inherit inputs; lib = self; }; }); + in { nixosConfigurations = import ./nixos inputs; @@ -40,8 +44,6 @@ packages.x86_64-linux = self.lib.nixosConfigurationsAsPackages.x86_64-linux // self.lib.homeConfigurationsAsPackages.x86_64-linux; - checks = self.packages; - - lib = import ./lib inputs; + lib = lib.my; }; } 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; } |