diff --git a/dev/flake-module.nix b/dev/flake-module.nix index 60da698..d4a8237 100644 --- a/dev/flake-module.nix +++ b/dev/flake-module.nix @@ -6,7 +6,7 @@ ../site/flake-module.nix ]; systems = [ "x86_64-linux" "aarch64-darwin" ]; - perSystem = system: { config, self', inputs', pkgs, ... }: { + perSystem = { config, self', inputs', pkgs, ... }: { devShells.default = pkgs.mkShell { nativeBuildInputs = [ diff --git a/dev/flake.lock b/dev/flake.lock index 2dcc47f..2a33091 100644 --- a/dev/flake.lock +++ b/dev/flake.lock @@ -20,11 +20,11 @@ "nixpkgs": "nixpkgs" }, "locked": { - "lastModified": 1653415319, - "narHash": "sha256-h+YUX7ZYujf73KL1R07LsEnhV7uV8oxSKEhH4bDj0Jc=", + "lastModified": 1653487348, + "narHash": "sha256-lWqxXMRqO8blpwhH2qvrFBVp6aZ7V+Z3xTqkNWKyLQw=", "owner": "hercules-ci", "repo": "hercules-ci-effects", - "rev": "1f904af1824e7393702daaf40a8fed56ebead0d7", + "rev": "0ab66b56d3f2b40ddfd981899d1757773632075f", "type": "github" }, "original": { @@ -73,11 +73,11 @@ ] }, "locked": { - "lastModified": 1638351750, - "narHash": "sha256-90OHC+11DIHD1QyMIPUuM3n6zjH5Mhc8RBZa0h8969A=", + "lastModified": 1653494825, + "narHash": "sha256-hMhnHZGRlJBX3yWIvxuYIBo4g+XQy/DJ4z7ti0IX7x0=", "owner": "hercules-ci", "repo": "pre-commit-hooks.nix", - "rev": "3bb289628867ac40fc9e7c514f8266b1ce8c0910", + "rev": "89b64b7aca69fbd2957232d6a5a36bb93ad59d4b", "type": "github" }, "original": { diff --git a/lib.nix b/lib.nix index bb693e5..0d728f3 100644 --- a/lib.nix +++ b/lib.nix @@ -2,8 +2,17 @@ let inherit (lib) mkOption + mkOptionType + defaultFunctor + isAttrs + isFunction + showOption types ; + inherit (lib.types) + path + submoduleWith + ; # Polyfill functionTo to make sure it has type merging. # Remove 2022-12 @@ -26,6 +35,32 @@ let nestedTypes.elemType = elemType; }; + # Polyfill https://github.com/NixOS/nixpkgs/pull/163617 + deferredModuleWith = lib.deferredModuleWith or ( + attrs@{ staticModules ? [ ] }: mkOptionType { + name = "deferredModule"; + description = "module"; + check = x: isAttrs x || isFunction x || path.check x; + merge = loc: defs: staticModules ++ map (def: lib.setDefaultModuleLocation "${def.file}, via option ${showOption loc}" def.value) defs; + inherit (submoduleWith { modules = staticModules; }) + getSubOptions + getSubModules; + substSubModules = m: deferredModuleWith (attrs // { + staticModules = m; + }); + functor = defaultFunctor "deferredModuleWith" // { + type = deferredModuleWith; + payload = { + inherit staticModules; + }; + binOp = lhs: rhs: { + staticModules = lhs.staticModules ++ rhs.staticModules; + }; + }; + } + ); + + flake-parts-lib = { evalFlakeModule = { self @@ -53,10 +88,9 @@ let mkPerSystemType = module: - functionTo (types.submoduleWith { - modules = [ module ]; - shorthandOnlyDefinesConfig = false; - }); + deferredModuleWith { + staticModules = [ module ]; + }; mkPerSystemOption = module: diff --git a/modules/nixpkgs.nix b/modules/nixpkgs.nix index d3870fc..44df915 100644 --- a/modules/nixpkgs.nix +++ b/modules/nixpkgs.nix @@ -13,7 +13,7 @@ # { config = { - perSystem = _: { inputs', lib, ... }: { + perSystem = { inputs', lib, ... }: { config = { _module.args.pkgs = lib.mkOptionDefault ( builtins.seq diff --git a/modules/perSystem.nix b/modules/perSystem.nix index 446adee..1e9be2c 100644 --- a/modules/perSystem.nix +++ b/modules/perSystem.nix @@ -34,6 +34,14 @@ in _module.args.self' = rootConfig.perInput system self; }; }); + apply = modules: system: + (lib.evalModules { + inherit modules; + prefix = [ "perSystem" system ]; + specialArgs = { + inherit system; + }; + }).config; }; allSystems = mkOption { @@ -44,7 +52,6 @@ in }; config = { - perSystem = system: { _module.args.system = system; }; allSystems = genAttrs config.systems config.perSystem; # TODO: Sub-optimal error message. Get Nix to support a memoization primop, or get Nix Flakes to support systems properly or get Nix Flakes to add a name to flakes. _module.args.getSystem = system: config.allSystems.${system} or (builtins.trace "using non-memoized system ${system}" config.perSystem system); diff --git a/site/flake-module.nix b/site/flake-module.nix index 4814b57..6cf4d8c 100644 --- a/site/flake-module.nix +++ b/site/flake-module.nix @@ -1,5 +1,5 @@ { ... }: { - perSystem = system: { config, self', inputs', pkgs, lib, ... }: + perSystem = { config, self', inputs', pkgs, lib, ... }: let inherit (lib) filter any hasPrefix concatMap removePrefix; diff --git a/template/default/flake.nix b/template/default/flake.nix index f0d6be7..9277a70 100644 --- a/template/default/flake.nix +++ b/template/default/flake.nix @@ -17,7 +17,7 @@ ]; systems = [ "x86_64-linux" "aarch64-darwin" ]; - perSystem = system: { config, self', inputs', pkgs, ... }: { + perSystem = { config, self', inputs', pkgs, system, ... }: { # Per-system attributes can be defined here. The self' and inputs' # module parameters provide easy access to attributes of the same # system. diff --git a/template/multi-module/flake.nix b/template/multi-module/flake.nix index 2964b21..91e334c 100644 --- a/template/multi-module/flake.nix +++ b/template/multi-module/flake.nix @@ -13,7 +13,7 @@ ./hello/flake-module.nix ]; systems = [ "x86_64-linux" "aarch64-darwin" ]; - perSystem = system: { config, self', inputs', ... }: { + perSystem = { config, self', inputs', ... }: { # Per-system attributes can be defined here. The self' and inputs' # module parameters provide easy access to attributes of the same # system. diff --git a/template/multi-module/hello/flake-module.nix b/template/multi-module/hello/flake-module.nix index 8629bfe..55066f0 100644 --- a/template/multi-module/hello/flake-module.nix +++ b/template/multi-module/hello/flake-module.nix @@ -1,7 +1,7 @@ # Definitions can be imported from a separate file like this one { self, ... }: { - perSystem = system: { config, self', inputs', pkgs, ... }: { + perSystem = { config, self', inputs', pkgs, ... }: { # Definitions like this are entirely equivalent to the ones # you may have directly in flake.nix. packages.hello = pkgs.hello;