From eac0970515bccba908c4bbea57911fd36e51c1a2 Mon Sep 17 00:00:00 2001 From: DavHau Date: Thu, 2 Mar 2023 21:34:42 +0700 Subject: [PATCH] feat(strict name/version): strictly require public.name and public.version to be set --- examples/flake-parts/derivation/flake.nix | 3 +- examples/flake-parts/htop/flake.nix | 4 +-- examples/flake-parts/htop/htop.nix | 11 +++---- examples/flake-parts/mkDerivation/flake.nix | 8 +++-- examples/no-flake/derivation/default.nix | 3 +- examples/no-flake/mkDerivation/default.nix | 9 +++--- lib/makeModule.nix | 15 ++++++++++ .../builtins-derivation/implementation.nix | 10 +++++-- .../builtins-derivation/interface.nix | 5 +--- .../drv-parts/mkDerivation/implementation.nix | 29 ++++++++----------- modules/drv-parts/mkDerivation/interface.nix | 2 -- modules/drv-parts/pkg-func/interface.nix | 6 ++-- 12 files changed, 59 insertions(+), 46 deletions(-) diff --git a/examples/flake-parts/derivation/flake.nix b/examples/flake-parts/derivation/flake.nix index a565b9b..dbfc251 100644 --- a/examples/flake-parts/derivation/flake.nix +++ b/examples/flake-parts/derivation/flake.nix @@ -24,9 +24,10 @@ # select mkDerivation as a backend for this package imports = [drv-parts.modules.drv-parts.builtins-derivation]; + public.name = "test"; + # set options builtins-derivation = { - name = "test"; args = ["-c" "echo $name > $out"]; builder = "/bin/sh"; system = system; diff --git a/examples/flake-parts/htop/flake.nix b/examples/flake-parts/htop/flake.nix index cf4015e..f8ef8ed 100644 --- a/examples/flake-parts/htop/flake.nix +++ b/examples/flake-parts/htop/flake.nix @@ -32,9 +32,7 @@ imports = [./htop.nix]; flags.sensorsSupport = false; deps = {nixpkgs, ...}: {inherit (nixpkgs) stdenv;}; - mkDerivation = { - pname = lib.mkForce "htop-mod"; - }; + public.name = lib.mkForce "htop-mod"; }; }; diff --git a/examples/flake-parts/htop/htop.nix b/examples/flake-parts/htop/htop.nix index e7e96d8..a1af642 100644 --- a/examples/flake-parts/htop/htop.nix +++ b/examples/flake-parts/htop/htop.nix @@ -31,15 +31,16 @@ in { systemdSupport = lib.mkDefault stdenv.isLinux; }; + public.name = "htop"; + public.version = "3.2.1"; + mkDerivation = { # set options - pname = "htop"; - version = "3.2.1"; src = deps.fetchFromGitHub { owner = "htop-dev"; - repo = config.mkDerivation.pname; - rev = config.mkDerivation.version; + repo = config.public.name; + rev = config.public.version; sha256 = "sha256-MwtsvdPHcUdegsYj9NGyded5XJQxXri1IM1j4gef1Xk="; }; @@ -70,7 +71,7 @@ in { license = licenses.gpl2Only; platforms = platforms.all; maintainers = with maintainers; [ rob relrod SuperSandro2000 ]; - changelog = "https://github.com/htop-dev/htop/blob/${version}/ChangeLog"; + changelog = "https://github.com/htop-dev/htop/blob/${config.public.version}/ChangeLog"; }; }; }; diff --git a/examples/flake-parts/mkDerivation/flake.nix b/examples/flake-parts/mkDerivation/flake.nix index 4471c5e..20ee938 100644 --- a/examples/flake-parts/mkDerivation/flake.nix +++ b/examples/flake-parts/mkDerivation/flake.nix @@ -19,7 +19,7 @@ perSystem = {config, pkgs, ...}: { checks = config.packages; - drvs.hello = { + drvs.hello = {config, ...}: { # select mkDerivation as a backend for this package imports = [drv-parts.modules.drv-parts.mkDerivation]; @@ -28,11 +28,13 @@ inherit (nixpkgs) stdenv; }; + public.name = "hello"; + public.version = "2.12.1"; + # set options mkDerivation = { - name = "hello"; src = pkgs.fetchurl { - url = "mirror://gnu/hello/hello-2.12.1.tar.gz"; + url = "mirror://gnu/hello/${config.public.name}-${config.public.version}.tar.gz"; sha256 = "sha256-jZkUKv2SV28wsM18tCqNxoCZmLxdYH2Idh9RLibH2yA="; }; }; diff --git a/examples/no-flake/derivation/default.nix b/examples/no-flake/derivation/default.nix index d877609..9cfad86 100644 --- a/examples/no-flake/derivation/default.nix +++ b/examples/no-flake/derivation/default.nix @@ -7,9 +7,10 @@ # select builtins.derivation as a backend for this package imports = [drv-parts.modules.drv-parts.builtins-derivation]; + public.name = "test"; + # set options builtins-derivation = { - name = "test"; builder = "/bin/sh"; args = ["-c" "echo $name > $out"]; system = builtins.currentSystem; diff --git a/examples/no-flake/mkDerivation/default.nix b/examples/no-flake/mkDerivation/default.nix index 38aadc6..82f65e9 100644 --- a/examples/no-flake/mkDerivation/default.nix +++ b/examples/no-flake/mkDerivation/default.nix @@ -13,13 +13,14 @@ enableFoo = "build with foo"; }; - mkDerivation = { - # set options - pname = + public.name = if config.flags.enableFoo then "hello-with-foo" else "hello"; - version = nixpkgs.hello.version; + + public.version = nixpkgs.hello.version; + + mkDerivation = { src = nixpkgs.hello.src; doCheck = true; }; diff --git a/lib/makeModule.nix b/lib/makeModule.nix index 0284e8f..fc01f44 100644 --- a/lib/makeModule.nix +++ b/lib/makeModule.nix @@ -125,6 +125,19 @@ in {config, options, extendModules, ...}: { }; userEnvModule = {config.env = config.env;}; + # drv-parts strictly requires name + version. If the original derivation + # only specified a `name` but no `version` or `pname`, we have to recover + # name and version from the original `name` by splitting it. + origNameSplit = l.splitString "-" origMkDrvArgs.name; + finalVersion = + if origMkDrvArgs ? version + then origMkDrvArgs.version + else l.last origNameSplit; + finalName = + if origMkDrvArgs ? pname + then origMkDrvArgs.pname + else l.removeSuffix finalVersion origMkDrvArgs.name; + # nested drv-parts evaluation to include the mkDerivation arguments # extracted from the default.nix package func finalDrvModule = { @@ -139,6 +152,8 @@ in {config, options, extendModules, ...}: { _file = "finalDrvModule"; options = flagOptions; config.deps.stdenv = config.deps.stdenv; + config.public.name = finalName; + config.public.version = finalVersion; }; finalDrvEval = l.evalModules { diff --git a/modules/drv-parts/builtins-derivation/implementation.nix b/modules/drv-parts/builtins-derivation/implementation.nix index 5f04558..2ef6185 100644 --- a/modules/drv-parts/builtins-derivation/implementation.nix +++ b/modules/drv-parts/builtins-derivation/implementation.nix @@ -39,7 +39,11 @@ in { config.final.package-func = lib.mkDefault builtins.derivation; - config.public.name = cfg.name; - - config.final.package-args = envChecked // finalArgs // {inherit outputs;}; + config.final.package-func-args = + envChecked + // finalArgs + // { + inherit outputs; + inherit (config.public) name; + }; } diff --git a/modules/drv-parts/builtins-derivation/interface.nix b/modules/drv-parts/builtins-derivation/interface.nix index 1c36747..5fceb3c 100644 --- a/modules/drv-parts/builtins-derivation/interface.nix +++ b/modules/drv-parts/builtins-derivation/interface.nix @@ -9,9 +9,6 @@ builder = lib.mkOption { type = t.oneOf [t.str t.path t.package]; }; - name = lib.mkOption { - type = t.str; - }; system = lib.mkOption { type = t.str; }; @@ -19,7 +16,7 @@ in { imports = [ - ../public/interface.nix + ../public ../pkg-func/interface.nix ]; diff --git a/modules/drv-parts/mkDerivation/implementation.nix b/modules/drv-parts/mkDerivation/implementation.nix index 35ad904..33a7756 100644 --- a/modules/drv-parts/mkDerivation/implementation.nix +++ b/modules/drv-parts/mkDerivation/implementation.nix @@ -30,21 +30,9 @@ Specify the top-level option instead, or rename the environment variable. ''; - name = - if cfg.pname != null - then "${cfg.pname}-${cfg.version}" - else if cfg.name != null - then cfg.name - else if cfg.final.package-func-result ? name - then cfg.final.package-func-result.name - else throw "Cannot determine package name"; - - derivation = - { - inherit name; - } + public = # meta - // (l.optionalAttrs (cfg.passthru ? meta) { + (l.optionalAttrs (cfg.passthru ? meta) { inherit (cfg.passthru) meta; }) # tests @@ -62,7 +50,14 @@ in { config.final.package-func = lib.mkDefault config.deps.stdenv.mkDerivation; # add mkDerivation specific derivation attributes - config.public = derivation; - - config.final.package-args = envChecked // finalArgs // {inherit outputs;}; + config.public = public; + + config.final.package-func-args = + envChecked + // finalArgs + // { + inherit outputs; + inherit (config.public) version; + pname = config.public.name; + }; } diff --git a/modules/drv-parts/mkDerivation/interface.nix b/modules/drv-parts/mkDerivation/interface.nix index 36f56ae..d576fff 100644 --- a/modules/drv-parts/mkDerivation/interface.nix +++ b/modules/drv-parts/mkDerivation/interface.nix @@ -76,8 +76,6 @@ # make-derivation args - without defaults enableParallelChecking = optNullOrBool; - name = optNullOrStr; - pname = optNullOrStr; realBuilder = optPackage; requiredSystemFeatures = optListOfStr; version = optNullOrStr; diff --git a/modules/drv-parts/pkg-func/interface.nix b/modules/drv-parts/pkg-func/interface.nix index dc3bc11..41c9ee3 100644 --- a/modules/drv-parts/pkg-func/interface.nix +++ b/modules/drv-parts/pkg-func/interface.nix @@ -12,14 +12,14 @@ in { description = "Outputs of the derivation this package function produces"; }; - final.package-args = l.mkOption { + final.package-func-args = l.mkOption { type = t.anything; description = "The arguments which will be passed to `final.package-func`"; }; final.package-func = l.mkOption { type = t.raw; - description = "Will be called with `final.package-args` in order to derive `final.package-func-result`"; + description = "Will be called with `final.package-func-args` in order to derive `final.package-func-result`"; }; final.package-func-result = l.mkOption { @@ -28,7 +28,7 @@ in { The result of calling the final derivation function. This is not necessarily the same as `final.package`. The function output might not be compatible to the interface of `final.package` and additional logic might be needed to create `final.package`. ''; - default = config.final.package-func config.final.package-args; + default = config.final.package-func config.final.package-func-args; readOnly = true; }; };