From 5215a657bff15e162a35371f91b0ed01e3db02e6 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Sun, 14 Jan 2024 12:59:39 -0800 Subject: [PATCH] Apply misc code cleanup --- builtinModules/apps.nix | 4 +- builtinModules/builtinOverlay.nix | 10 +-- builtinModules/bundlers.nix | 4 +- builtinModules/checks.nix | 8 +- builtinModules/core.nix | 4 +- builtinModules/homeConfigurations.nix | 48 ++++------ builtinModules/nixosConfigurations.nix | 44 ++++------ builtinModules/propagationModule.nix | 10 +-- default.nix | 117 +++++++++++++------------ 9 files changed, 112 insertions(+), 137 deletions(-) diff --git a/builtinModules/apps.nix b/builtinModules/apps.nix index 8a43a79..99bedf2 100644 --- a/builtinModules/apps.nix +++ b/builtinModules/apps.nix @@ -11,9 +11,7 @@ let isApp = x: (x ? type) && (x.type == "app") && (x ? program); mkApp = pkgs: app: - let - app' = if isFunction app then app pkgs else app; - in + let app' = if isFunction app then app pkgs else app; in if isApp app' then app' else { type = "app"; program = "${app'}"; }; in { diff --git a/builtinModules/builtinOverlay.nix b/builtinModules/builtinOverlay.nix index 93b02d2..19dd23f 100644 --- a/builtinModules/builtinOverlay.nix +++ b/builtinModules/builtinOverlay.nix @@ -7,6 +7,7 @@ let inherit (lib) isList mkOption mkOrder mapAttrs optionalAttrs; inherit (lib.types) listOf nullOr oneOf str; inherit (builtins) pathExists; + inherit (flakelight) selectAttr; in { options = { @@ -25,13 +26,10 @@ in }; config.withOverlays = mkOrder 10 (final: prev: - let - inherit (prev.stdenv.hostPlatform) system; - in - { + let inherit (prev.stdenv.hostPlatform) system; in { inherit system moduleArgs src inputs outputs flakelight; - inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs; - outputs' = mapAttrs (_: v: v.${system} or { }) outputs; + inputs' = mapAttrs (_: selectAttr system) inputs; + outputs' = selectAttr system outputs; defaultMeta = { platforms = config.systems; diff --git a/builtinModules/bundlers.nix b/builtinModules/bundlers.nix index 451cc79..93f17ff 100644 --- a/builtinModules/bundlers.nix +++ b/builtinModules/bundlers.nix @@ -9,7 +9,9 @@ let inherit (flakelight.types) function optFunctionTo; wrapBundler = pkgs: bundler: drv: - if isFunction (bundler (pkgs // drv)) then bundler pkgs drv else bundler drv; + if isFunction (bundler (pkgs // drv)) + then bundler pkgs drv + else bundler drv; in { options = { diff --git a/builtinModules/checks.nix b/builtinModules/checks.nix index 2816a71..d0bac18 100644 --- a/builtinModules/checks.nix +++ b/builtinModules/checks.nix @@ -8,10 +8,8 @@ let inherit (lib.types) lazyAttrsOf nullOr raw; inherit (flakelight.types) optFunctionTo; - mkCheck = pkgs: src: name: cmd: - let - cmd' = if isFunction cmd then cmd pkgs else cmd; - in + mkCheck = pkgs: name: cmd: + let cmd' = if isFunction cmd then cmd pkgs else cmd; in if isDerivation cmd' then cmd' else pkgs.runCommand "check-${name}" { } '' cp --no-preserve=mode -r ${src} src @@ -28,6 +26,6 @@ in config.outputs = mkIf (config.checks != null) { checks = genSystems (pkgs: - mapAttrs (mkCheck pkgs src) (config.checks pkgs)); + mapAttrs (mkCheck pkgs) (config.checks pkgs)); }; } diff --git a/builtinModules/core.nix b/builtinModules/core.nix index 63e22e2..5b7fb23 100644 --- a/builtinModules/core.nix +++ b/builtinModules/core.nix @@ -18,7 +18,9 @@ let if (length defs) == 1 then (head defs).value else if all isAttrs (getValues defs) then (lazyAttrsOf outputs).merge loc defs - else throw "The option `${showOption loc}' has conflicting definitions in ${showFiles (getFiles defs)}"; + else + throw ("The option `${showOption loc}' has conflicting definitions" + + " in ${showFiles (getFiles defs)}"); }; pkgsFor = genAttrs config.systems (system: import inputs.nixpkgs { diff --git a/builtinModules/homeConfigurations.nix b/builtinModules/homeConfigurations.nix index 5008279..f120916 100644 --- a/builtinModules/homeConfigurations.nix +++ b/builtinModules/homeConfigurations.nix @@ -4,41 +4,29 @@ { config, lib, inputs, flakelight, moduleArgs, ... }: let - inherit (builtins) concatLists head mapAttrs match; - inherit (lib) foldl last mapAttrsToList mkOption mkIf recursiveUpdate - zipAttrsWith; + inherit (builtins) head mapAttrs match; + inherit (lib) foldl mapAttrsToList mkOption mkIf recursiveUpdate; inherit (lib.types) attrs lazyAttrsOf; + inherit (flakelight) selectAttr; inherit (flakelight.types) optFunctionTo; isHome = x: x ? activationPackage; - mergeCfg = zipAttrsWith (n: vs: - if n == "extraSpecialArgs" then - foldl (a: b: a // b) { } vs - else if n == "modules" then - concatLists vs - else last vs); - - mkHome = name: cfg: - let - inherit (cfg) system; - in - inputs.home-manager.lib.homeManagerConfiguration (mergeCfg [ - { - extraSpecialArgs = { - inherit inputs; - inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs; - }; - modules = [ - ({ lib, ... }: { - home.username = lib.mkDefault (head (match "([^@]*)(@.*)?" name)); - }) - config.propagationModule - ]; - pkgs = inputs.nixpkgs.legacyPackages.${system}; - } - (removeAttrs cfg [ "system" ]) - ]); + mkHome = name: cfg: inputs.home-manager.lib.homeManagerConfiguration ( + (removeAttrs cfg [ "system" ]) // { + extraSpecialArgs = { + inherit inputs; + inputs' = mapAttrs (_: selectAttr cfg.system) inputs; + } // cfg.extraSpecialArgs or { }; + modules = [ + ({ lib, ... }: { + home.username = lib.mkDefault (head (match "([^@]*)(@.*)?" name)); + }) + config.propagationModule + ] ++ cfg.modules or [ ]; + pkgs = inputs.nixpkgs.legacyPackages.${cfg.system}; + } + ); configs = mapAttrs (name: f: diff --git a/builtinModules/nixosConfigurations.nix b/builtinModules/nixosConfigurations.nix index 1bd6024..30b4bb6 100644 --- a/builtinModules/nixosConfigurations.nix +++ b/builtinModules/nixosConfigurations.nix @@ -4,42 +4,28 @@ { config, lib, inputs, flakelight, moduleArgs, ... }: let - inherit (builtins) concatLists mapAttrs; - inherit (lib) foldl last mapAttrsToList mkIf mkOption recursiveUpdate - zipAttrsWith; + inherit (builtins) mapAttrs; + inherit (lib) foldl mapAttrsToList mkIf mkOption recursiveUpdate; inherit (lib.types) attrs lazyAttrsOf; + inherit (flakelight) selectAttr; inherit (flakelight.types) optFunctionTo; # Avoid checking if toplevel is a derivation as it causes the nixos modules # to be evaluated. isNixos = x: x ? config.system.build.toplevel; - mergeCfg = zipAttrsWith (n: vs: - if n == "specialArgs" then - foldl (a: b: a // b) { } vs - else if n == "modules" then - concatLists vs - else last vs); + mkNixos = hostname: cfg: inputs.nixpkgs.lib.nixosSystem (cfg // { + specialArgs = { + inherit inputs hostname; + inputs' = mapAttrs (_: selectAttr cfg.system) inputs; + } // cfg.specialArgs or { }; + modules = [ config.propagationModule ] ++ cfg.modules or [ ]; + }); - mkSystem = hostname: cfg: - let - inherit (cfg) system; - in - inputs.nixpkgs.lib.nixosSystem (mergeCfg [ - { - specialArgs = { - inherit inputs hostname; - inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs; - }; - modules = [ config.propagationModule ]; - } - cfg - ]); - - systems = mapAttrs + configs = mapAttrs (hostname: f: let val = f moduleArgs; in - if isNixos val then val else mkSystem hostname val) + if isNixos val then val else mkNixos hostname val) config.nixosConfigurations; in { @@ -49,13 +35,15 @@ in }; config.outputs = mkIf (config.nixosConfigurations != { }) { - nixosConfigurations = systems; + nixosConfigurations = configs; checks = foldl recursiveUpdate { } (mapAttrsToList (n: v: { + # Wrapping the drv is needed as computing its name is expensive + # If not wrapped, it slows down `nix flake show` significantly ${v.config.nixpkgs.system}."nixos-${n}" = v.pkgs.runCommand "check-nixos-${n}" { } "echo ${v.config.system.build.toplevel} > $out"; }) - systems); + configs); }; } diff --git a/builtinModules/propagationModule.nix b/builtinModules/propagationModule.nix index b1f1953..22f704f 100644 --- a/builtinModules/propagationModule.nix +++ b/builtinModules/propagationModule.nix @@ -8,6 +8,7 @@ { lib, config, flakelight, moduleArgs, inputs, outputs, ... }: let inherit (lib) mapAttrs mkOption optionalAttrs; + inherit (flakelight) selectAttr; inherit (flakelight.types) module; in { @@ -15,10 +16,7 @@ in config.propagationModule = { lib, pkgs, options, ... }: - let - inherit (pkgs.stdenv.hostPlatform) system; - in - { + let inherit (pkgs.stdenv.hostPlatform) system; in { config = (optionalAttrs (options ? nixpkgs.overlays) { # Apply flakelight overlays to NixOS/home-manager configurations nixpkgs.overlays = lib.mkOrder 10 @@ -32,8 +30,8 @@ in # Give access to flakelight module args under `flake` arg. # Also include inputs'/outputs' which depend on `pkgs`. _module.args.flake = { - inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs; - outputs' = mapAttrs (_: v: v.${system} or { }) outputs; + inputs' = mapAttrs (_: selectAttr system) inputs; + outputs' = selectAttr system outputs; } // moduleArgs; }; }; diff --git a/default.nix b/default.nix index 043c011..7df750a 100644 --- a/default.nix +++ b/default.nix @@ -6,10 +6,10 @@ inputs: let inherit (inputs) nixpkgs; inherit (builtins) isAttrs isPath readDir; - inherit (nixpkgs.lib) attrNames composeManyExtensions - filter findFirst fix genAttrs getValues hasSuffix isFunction isList - mapAttrsToList pathExists pipe removePrefix removeSuffix evalModules - mkDefault mkOptionType singleton; + inherit (nixpkgs.lib) attrNames composeManyExtensions evalModules filter + findFirst fix genAttrs getValues hasSuffix isFunction isList mapAttrs + mapAttrsToList mkDefault mkOptionType pathExists pipe removePrefix + removeSuffix singleton; inherit (nixpkgs.lib.types) coercedTo functionTo listOf; inherit (nixpkgs.lib.options) mergeEqualOption mergeOneOption; @@ -36,61 +36,62 @@ let }; flakelight = { - inherit mkFlake supportedSystem autoImport autoImportArgs; + inherit autoImport autoImportArgs mkFlake selectAttr supportedSystem + types; + }; - types = { - overlay = mkOptionType { - name = "overlay"; - description = "nixpkgs overlay"; - descriptionClass = "noun"; - check = isFunction; - merge = _: defs: composeManyExtensions (getValues defs); - }; - - packageDef = mkOptionType { - name = "packageDef"; - description = "package definition"; - descriptionClass = "noun"; - check = isFunction; - merge = mergeOneOption; - }; - - path = mkOptionType { - name = "path"; - description = "path"; - descriptionClass = "noun"; - check = isPath; - merge = mergeEqualOption; - }; - - function = mkOptionType { - name = "function"; - description = "function"; - descriptionClass = "noun"; - check = isFunction; - merge = mergeOneOption; - }; - - module = mkOptionType { - name = "module"; - description = "module"; - descriptionClass = "noun"; - check = x: isPath x || isFunction x || isAttrs x; - merge = _: defs: { imports = getValues defs; }; - }; - - fileset = mkOptionType { - name = "fileset"; - description = "fileset"; - descriptionClass = "noun"; - check = x: isPath x || x._type or null == "fileset"; - }; - - optListOf = elemType: coercedTo elemType singleton (listOf elemType); - - optFunctionTo = elemType: coercedTo elemType (x: _: x) - (functionTo elemType); + types = { + overlay = mkOptionType { + name = "overlay"; + description = "nixpkgs overlay"; + descriptionClass = "noun"; + check = isFunction; + merge = _: defs: composeManyExtensions (getValues defs); }; + + packageDef = mkOptionType { + name = "packageDef"; + description = "package definition"; + descriptionClass = "noun"; + check = isFunction; + merge = mergeOneOption; + }; + + path = mkOptionType { + name = "path"; + description = "path"; + descriptionClass = "noun"; + check = isPath; + merge = mergeEqualOption; + }; + + function = mkOptionType { + name = "function"; + description = "function"; + descriptionClass = "noun"; + check = isFunction; + merge = mergeOneOption; + }; + + module = mkOptionType { + name = "module"; + description = "module"; + descriptionClass = "noun"; + check = x: isPath x || isFunction x || isAttrs x; + merge = _: defs: { imports = getValues defs; }; + }; + + fileset = mkOptionType { + name = "fileset"; + description = "fileset"; + descriptionClass = "noun"; + check = x: isPath x || x._type or null == "fileset"; + }; + + optListOf = elemType: coercedTo elemType singleton (listOf elemType); + + optFunctionTo = elemType: coercedTo elemType (x: _: x) + (functionTo elemType); }; supportedSystem = { lib, stdenv, ... }: @@ -125,5 +126,7 @@ let autoImportArgs = dir: args: name: let v = autoImport dir name; in if isFunction v then v args else v; + + selectAttr = attr: mapAttrs (_: v: v.${attr} or { }); in flakelight