Apply misc code cleanup

This commit is contained in:
Archit Gupta 2024-01-14 12:59:39 -08:00
parent cc2f42fbdf
commit 5215a657bf
9 changed files with 112 additions and 137 deletions

View File

@ -11,9 +11,7 @@ let
isApp = x: (x ? type) && (x.type == "app") && (x ? program); isApp = x: (x ? type) && (x.type == "app") && (x ? program);
mkApp = pkgs: app: mkApp = pkgs: app:
let let app' = if isFunction app then app pkgs else app; in
app' = if isFunction app then app pkgs else app;
in
if isApp app' then app' else { type = "app"; program = "${app'}"; }; if isApp app' then app' else { type = "app"; program = "${app'}"; };
in in
{ {

View File

@ -7,6 +7,7 @@ let
inherit (lib) isList mkOption mkOrder mapAttrs optionalAttrs; inherit (lib) isList mkOption mkOrder mapAttrs optionalAttrs;
inherit (lib.types) listOf nullOr oneOf str; inherit (lib.types) listOf nullOr oneOf str;
inherit (builtins) pathExists; inherit (builtins) pathExists;
inherit (flakelight) selectAttr;
in in
{ {
options = { options = {
@ -25,13 +26,10 @@ in
}; };
config.withOverlays = mkOrder 10 (final: prev: config.withOverlays = mkOrder 10 (final: prev:
let let inherit (prev.stdenv.hostPlatform) system; in {
inherit (prev.stdenv.hostPlatform) system;
in
{
inherit system moduleArgs src inputs outputs flakelight; inherit system moduleArgs src inputs outputs flakelight;
inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs; inputs' = mapAttrs (_: selectAttr system) inputs;
outputs' = mapAttrs (_: v: v.${system} or { }) outputs; outputs' = selectAttr system outputs;
defaultMeta = { defaultMeta = {
platforms = config.systems; platforms = config.systems;

View File

@ -9,7 +9,9 @@ let
inherit (flakelight.types) function optFunctionTo; inherit (flakelight.types) function optFunctionTo;
wrapBundler = pkgs: bundler: drv: 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 in
{ {
options = { options = {

View File

@ -8,10 +8,8 @@ let
inherit (lib.types) lazyAttrsOf nullOr raw; inherit (lib.types) lazyAttrsOf nullOr raw;
inherit (flakelight.types) optFunctionTo; inherit (flakelight.types) optFunctionTo;
mkCheck = pkgs: src: name: cmd: mkCheck = pkgs: name: cmd:
let let cmd' = if isFunction cmd then cmd pkgs else cmd; in
cmd' = if isFunction cmd then cmd pkgs else cmd;
in
if isDerivation cmd' then cmd' else if isDerivation cmd' then cmd' else
pkgs.runCommand "check-${name}" { } '' pkgs.runCommand "check-${name}" { } ''
cp --no-preserve=mode -r ${src} src cp --no-preserve=mode -r ${src} src
@ -28,6 +26,6 @@ in
config.outputs = mkIf (config.checks != null) { config.outputs = mkIf (config.checks != null) {
checks = genSystems (pkgs: checks = genSystems (pkgs:
mapAttrs (mkCheck pkgs src) (config.checks pkgs)); mapAttrs (mkCheck pkgs) (config.checks pkgs));
}; };
} }

View File

@ -18,7 +18,9 @@ let
if (length defs) == 1 then (head defs).value if (length defs) == 1 then (head defs).value
else if all isAttrs (getValues defs) then else if all isAttrs (getValues defs) then
(lazyAttrsOf outputs).merge loc defs (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 { pkgsFor = genAttrs config.systems (system: import inputs.nixpkgs {

View File

@ -4,41 +4,29 @@
{ config, lib, inputs, flakelight, moduleArgs, ... }: { config, lib, inputs, flakelight, moduleArgs, ... }:
let let
inherit (builtins) concatLists head mapAttrs match; inherit (builtins) head mapAttrs match;
inherit (lib) foldl last mapAttrsToList mkOption mkIf recursiveUpdate inherit (lib) foldl mapAttrsToList mkOption mkIf recursiveUpdate;
zipAttrsWith;
inherit (lib.types) attrs lazyAttrsOf; inherit (lib.types) attrs lazyAttrsOf;
inherit (flakelight) selectAttr;
inherit (flakelight.types) optFunctionTo; inherit (flakelight.types) optFunctionTo;
isHome = x: x ? activationPackage; isHome = x: x ? activationPackage;
mergeCfg = zipAttrsWith (n: vs: mkHome = name: cfg: inputs.home-manager.lib.homeManagerConfiguration (
if n == "extraSpecialArgs" then (removeAttrs cfg [ "system" ]) // {
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 = { extraSpecialArgs = {
inherit inputs; inherit inputs;
inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs; inputs' = mapAttrs (_: selectAttr cfg.system) inputs;
}; } // cfg.extraSpecialArgs or { };
modules = [ modules = [
({ lib, ... }: { ({ lib, ... }: {
home.username = lib.mkDefault (head (match "([^@]*)(@.*)?" name)); home.username = lib.mkDefault (head (match "([^@]*)(@.*)?" name));
}) })
config.propagationModule config.propagationModule
]; ] ++ cfg.modules or [ ];
pkgs = inputs.nixpkgs.legacyPackages.${system}; pkgs = inputs.nixpkgs.legacyPackages.${cfg.system};
} }
(removeAttrs cfg [ "system" ]) );
]);
configs = mapAttrs configs = mapAttrs
(name: f: (name: f:

View File

@ -4,42 +4,28 @@
{ config, lib, inputs, flakelight, moduleArgs, ... }: { config, lib, inputs, flakelight, moduleArgs, ... }:
let let
inherit (builtins) concatLists mapAttrs; inherit (builtins) mapAttrs;
inherit (lib) foldl last mapAttrsToList mkIf mkOption recursiveUpdate inherit (lib) foldl mapAttrsToList mkIf mkOption recursiveUpdate;
zipAttrsWith;
inherit (lib.types) attrs lazyAttrsOf; inherit (lib.types) attrs lazyAttrsOf;
inherit (flakelight) selectAttr;
inherit (flakelight.types) optFunctionTo; inherit (flakelight.types) optFunctionTo;
# Avoid checking if toplevel is a derivation as it causes the nixos modules # Avoid checking if toplevel is a derivation as it causes the nixos modules
# to be evaluated. # to be evaluated.
isNixos = x: x ? config.system.build.toplevel; isNixos = x: x ? config.system.build.toplevel;
mergeCfg = zipAttrsWith (n: vs: mkNixos = hostname: cfg: inputs.nixpkgs.lib.nixosSystem (cfg // {
if n == "specialArgs" then
foldl (a: b: a // b) { } vs
else if n == "modules" then
concatLists vs
else last vs);
mkSystem = hostname: cfg:
let
inherit (cfg) system;
in
inputs.nixpkgs.lib.nixosSystem (mergeCfg [
{
specialArgs = { specialArgs = {
inherit inputs hostname; inherit inputs hostname;
inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs; inputs' = mapAttrs (_: selectAttr cfg.system) inputs;
}; } // cfg.specialArgs or { };
modules = [ config.propagationModule ]; modules = [ config.propagationModule ] ++ cfg.modules or [ ];
} });
cfg
]);
systems = mapAttrs configs = mapAttrs
(hostname: f: (hostname: f:
let val = f moduleArgs; in 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; config.nixosConfigurations;
in in
{ {
@ -49,13 +35,15 @@ in
}; };
config.outputs = mkIf (config.nixosConfigurations != { }) { config.outputs = mkIf (config.nixosConfigurations != { }) {
nixosConfigurations = systems; nixosConfigurations = configs;
checks = foldl recursiveUpdate { } (mapAttrsToList checks = foldl recursiveUpdate { } (mapAttrsToList
(n: v: { (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 ${v.config.nixpkgs.system}."nixos-${n}" = v.pkgs.runCommand
"check-nixos-${n}" "check-nixos-${n}"
{ } "echo ${v.config.system.build.toplevel} > $out"; { } "echo ${v.config.system.build.toplevel} > $out";
}) })
systems); configs);
}; };
} }

View File

@ -8,6 +8,7 @@
{ lib, config, flakelight, moduleArgs, inputs, outputs, ... }: { lib, config, flakelight, moduleArgs, inputs, outputs, ... }:
let let
inherit (lib) mapAttrs mkOption optionalAttrs; inherit (lib) mapAttrs mkOption optionalAttrs;
inherit (flakelight) selectAttr;
inherit (flakelight.types) module; inherit (flakelight.types) module;
in in
{ {
@ -15,10 +16,7 @@ in
config.propagationModule = config.propagationModule =
{ lib, pkgs, options, ... }: { lib, pkgs, options, ... }:
let let inherit (pkgs.stdenv.hostPlatform) system; in {
inherit (pkgs.stdenv.hostPlatform) system;
in
{
config = (optionalAttrs (options ? nixpkgs.overlays) { config = (optionalAttrs (options ? nixpkgs.overlays) {
# Apply flakelight overlays to NixOS/home-manager configurations # Apply flakelight overlays to NixOS/home-manager configurations
nixpkgs.overlays = lib.mkOrder 10 nixpkgs.overlays = lib.mkOrder 10
@ -32,8 +30,8 @@ in
# Give access to flakelight module args under `flake` arg. # Give access to flakelight module args under `flake` arg.
# Also include inputs'/outputs' which depend on `pkgs`. # Also include inputs'/outputs' which depend on `pkgs`.
_module.args.flake = { _module.args.flake = {
inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs; inputs' = mapAttrs (_: selectAttr system) inputs;
outputs' = mapAttrs (_: v: v.${system} or { }) outputs; outputs' = selectAttr system outputs;
} // moduleArgs; } // moduleArgs;
}; };
}; };

View File

@ -6,10 +6,10 @@ inputs:
let let
inherit (inputs) nixpkgs; inherit (inputs) nixpkgs;
inherit (builtins) isAttrs isPath readDir; inherit (builtins) isAttrs isPath readDir;
inherit (nixpkgs.lib) attrNames composeManyExtensions inherit (nixpkgs.lib) attrNames composeManyExtensions evalModules filter
filter findFirst fix genAttrs getValues hasSuffix isFunction isList findFirst fix genAttrs getValues hasSuffix isFunction isList mapAttrs
mapAttrsToList pathExists pipe removePrefix removeSuffix evalModules mapAttrsToList mkDefault mkOptionType pathExists pipe removePrefix
mkDefault mkOptionType singleton; removeSuffix singleton;
inherit (nixpkgs.lib.types) coercedTo functionTo listOf; inherit (nixpkgs.lib.types) coercedTo functionTo listOf;
inherit (nixpkgs.lib.options) mergeEqualOption mergeOneOption; inherit (nixpkgs.lib.options) mergeEqualOption mergeOneOption;
@ -36,7 +36,9 @@ let
}; };
flakelight = { flakelight = {
inherit mkFlake supportedSystem autoImport autoImportArgs; inherit autoImport autoImportArgs mkFlake selectAttr supportedSystem
types;
};
types = { types = {
overlay = mkOptionType { overlay = mkOptionType {
@ -91,7 +93,6 @@ let
optFunctionTo = elemType: coercedTo elemType (x: _: x) optFunctionTo = elemType: coercedTo elemType (x: _: x)
(functionTo elemType); (functionTo elemType);
}; };
};
supportedSystem = { lib, stdenv, ... }: supportedSystem = { lib, stdenv, ... }:
lib.meta.availableOn stdenv.hostPlatform; lib.meta.availableOn stdenv.hostPlatform;
@ -125,5 +126,7 @@ let
autoImportArgs = dir: args: name: autoImportArgs = dir: args: name:
let v = autoImport dir name; in let v = autoImport dir name; in
if isFunction v then v args else v; if isFunction v then v args else v;
selectAttr = attr: mapAttrs (_: v: v.${attr} or { });
in in
flakelight flakelight