From 98c4d12843143d1e51ae39f817dcf0f18c7ea5d1 Mon Sep 17 00:00:00 2001 From: Gerg-L Date: Tue, 9 Jul 2024 12:02:04 -0400 Subject: [PATCH] mkPackages: automagically wrapping with hello as a test package --- lib/default.nix | 69 ++++++++++++++++++++++++++++---------- packages/hello/call.nix | 7 ++++ packages/hello/package.nix | 19 +++++++++++ packages/hello/wrapper.nix | 17 ++++++++++ 4 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 packages/hello/call.nix create mode 100644 packages/hello/package.nix create mode 100644 packages/hello/wrapper.nix diff --git a/lib/default.nix b/lib/default.nix index ccf8401..0c9de1a 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -204,39 +204,72 @@ rec { / -> packages named by directory //call.nix -> callPackage override imported via import pkgs call.nix example - pkgs: { - inherit (pkgs.python3Packages) callPackage; + {python3Packages}: { + inherit (python3Packages) callPackage; args = {}; } //package.nix -> the package itself + //wrapper.nix: + a optional wrapper for the package + which is callPackage'd with the original package + as an argument named -unwrapped */ mkPackages = path: pkgs: lib.pipe path [ builtins.readDir (lib.filterAttrs (_: v: v == "directory")) - (lib.mapAttrs ( - n: _: + builtins.attrNames + (map ( + n: let - callPackage = lib.callPackageWith ( - pkgs - // { - inputs = constructInputs' pkgs.system inputs; - # maybe add self? - # inherit self; - # npins sources if i need them - # sources = import ./npins; - } - ); + p = "${path}/${n}"; + + callPackage = + file: args: + let + defaultArgs = + pkgs + // ( + let + inputs' = constructInputs' pkgs.stdenv.system inputs; + in + { + inherit inputs' inputs; + self' = inputs'.self; + inherit (inputs) self; + # npins sources if i ever use them + # sources = lib.mapAttrs (_: pkgs.npins.mkSource) (lib.importJSON "${self}/packages/sources.json").pins; + } + ); + _callPackage = lib.callPackageWith defaultArgs; + fullPath = "${p}/${file}.nix"; + callPath = "${p}/call.nix"; + in + assert lib.assertMsg (builtins.pathExists fullPath) + "File attempting to be callPackage'd '${fullPath}' does not exist"; + + if builtins.pathExists callPath then + let + x = _callPackage callPath { }; + in + x.callPackage or _callPackage fullPath (x.args or defaultArgs // args) + + else + _callPackage fullPath args; in - if builtins.pathExists "${path}/${n}/call.nix" then + + if builtins.pathExists "${p}/wrapper.nix" then + # My distaste for rec grows ever stronger let - x = import "${path}/${n}/call.nix" pkgs; + set."${n}-unwrapped" = callPackage "package" { }; in - x.callPackage "${path}/${n}/package.nix" x.args + { ${n} = callPackage "wrapper" set; } // set else - callPackage "${path}/${n}/package.nix" { } + { ${n} = callPackage "package" { }; } + )) + lib.mergeAttrsList ]; } diff --git a/packages/hello/call.nix b/packages/hello/call.nix new file mode 100644 index 0000000..8796be5 --- /dev/null +++ b/packages/hello/call.nix @@ -0,0 +1,7 @@ +{ callPackage }: +{ + callPackage = f: x: callPackage f ({ bar = "foo"; } // x); + args = { + foo = "bar"; + }; +} diff --git a/packages/hello/package.nix b/packages/hello/package.nix new file mode 100644 index 0000000..8cf3e2d --- /dev/null +++ b/packages/hello/package.nix @@ -0,0 +1,19 @@ +{ + stdenv, + fetchurl, + foo, + bar, +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "hello"; + version = "2.12.1"; + + src = fetchurl { + url = "mirror://gnu/hello/hello-${finalAttrs.version}.tar.gz"; + hash = "sha256-jZkUKv2SV28wsM18tCqNxoCZmLxdYH2Idh9RLibH2yA="; + }; + passthru = { + inherit foo bar; + }; +}) diff --git a/packages/hello/wrapper.nix b/packages/hello/wrapper.nix new file mode 100644 index 0000000..f26377f --- /dev/null +++ b/packages/hello/wrapper.nix @@ -0,0 +1,17 @@ +{ + symlinkJoin, + foo, + bar, + hello-unwrapped, +}: +symlinkJoin { + name = "hello-wrapped"; + paths = [ hello-unwrapped ]; + postBuild = '' + ln -s $out/bin/hello $out/bin/hello-wrapped + ''; + + passthru = { + inherit foo bar; + }; +}