diff --git a/builtinModules/packages.nix b/builtinModules/packages.nix index 9f68984..2898788 100644 --- a/builtinModules/packages.nix +++ b/builtinModules/packages.nix @@ -6,9 +6,9 @@ let inherit (builtins) hasAttr parseDrvName tryEval; inherit (lib) findFirst functionArgs mapAttrs' mapAttrs mkIf mkMerge mkOption - nameValuePair optionalAttrs; + nameValuePair optionalAttrs optionals; inherit (lib.types) lazyAttrsOf str uniq; - inherit (flakelight.types) nullable optCallWith overlay packageDef; + inherit (flakelight.types) nullable optFunctionTo overlay packageDef; genPkg = final: prev: name: pkg: let @@ -27,6 +27,8 @@ let final.callPackage pkg' overrides; genPkgs = final: prev: pkgs: mapAttrs (name: genPkg final prev name) pkgs; + + getPkgDefs = pkgs: config.packages (moduleArgs // { inherit (pkgs) system; }); in { options = { @@ -36,8 +38,8 @@ in }; packages = mkOption { - type = optCallWith moduleArgs (lazyAttrsOf packageDef); - default = { }; + type = nullable (optFunctionTo (lazyAttrsOf packageDef)); + default = null; }; pname = mkOption { @@ -57,9 +59,10 @@ in packages.default = config.package; }) - (mkIf (config.packages != { }) { + (mkIf (config.packages != null) { packageOverlay = final: prev: let + pkgDefs = getPkgDefs prev; getName = pkg: pkg.pname or (parseDrvName pkg.name).name; mockPkgs = import ../misc/nameMockedPkgs.nix prev; @@ -68,20 +71,19 @@ in "please set the `pname` flakelight option to the intended name.")) [ (assert config.pname != null; config.pname) - (getName (mockPkgs.callPackage config.packages.default { })) + (getName (mockPkgs.callPackage pkgDefs.default { })) (getName (import inputs.nixpkgs { inherit (prev.stdenv.hostPlatform) system; inherit (config.nixpkgs) config; overlays = config.withOverlays ++ - [ (final: prev: genPkgs final prev config.packages) ]; + [ (final: prev: genPkgs final prev pkgDefs) ]; }).default) ]; in - (optionalAttrs (config.packages ? default) rec { - default = genPkg final prev defaultPkgName - config.packages.default; + (optionalAttrs (pkgDefs ? default) rec { + default = genPkg final prev defaultPkgName pkgDefs.default; ${defaultPkgName} = default; - }) // genPkgs final prev (removeAttrs config.packages [ "default" ]); + }) // genPkgs final prev (removeAttrs pkgDefs [ "default" ]); overlay = final: prev: removeAttrs (config.packageOverlay (final.appendOverlays config.withOverlays) prev) @@ -89,16 +91,15 @@ in outputs = rec { packages = genSystems (pkgs: - mapAttrs (k: _: pkgs.${k}) config.packages); + mapAttrs (k: _: pkgs.${k}) (getPkgDefs pkgs)); checks = mapAttrs (_: mapAttrs' (n: nameValuePair ("packages-" + n))) packages; }; - }) - (mkIf (config.packages ? default) { - devShell.inputsFrom = pkgs: [ pkgs.default ]; + devShell.inputsFrom = pkgs: + optionals ((getPkgDefs pkgs) ? default) [ pkgs.default ]; }) ]; } diff --git a/tests/default.nix b/tests/default.nix index 22c83e3..207f037 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -287,6 +287,20 @@ in }) (f: (import f.packages.x86_64-linux.pkg2)); + packages-fn-has-system = test + (flakelight ./empty { + packages = { system, ... }: (if system == "x86_64-linux" then { + default = { stdenv }: + stdenv.mkDerivation { + name = "pkg1"; + src = ./empty; + installPhase = "echo true > $out"; + }; + } else { }); + }) + (f: (import f.packages.x86_64-linux.default) + && !(f.packages.aarch64-linux ? default)); + legacyPackages-set-pkgs = test (flakelight ./empty { inputs = { inherit nixpkgs; };