Skip to content

Commit

Permalink
feat(strict name/version): strictly require public.name and public.ve…
Browse files Browse the repository at this point in the history
…rsion to be set
  • Loading branch information
DavHau committed Mar 2, 2023
1 parent da20961 commit 939ad4d
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 43 deletions.
3 changes: 2 additions & 1 deletion examples/flake-parts/derivation/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions examples/flake-parts/htop/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};
};

Expand Down
11 changes: 6 additions & 5 deletions examples/flake-parts/htop/htop.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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=";
};

Expand Down Expand Up @@ -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";
};
};
};
Expand Down
8 changes: 5 additions & 3 deletions examples/flake-parts/mkDerivation/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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=";
};
};
Expand Down
3 changes: 2 additions & 1 deletion examples/no-flake/derivation/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions examples/no-flake/mkDerivation/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
15 changes: 15 additions & 0 deletions lib/makeModule.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions modules/drv-parts/builtins-derivation/implementation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ in {

config.package-func = lib.mkDefault builtins.derivation;

config.public.name = cfg.name;

config.package-func-args = envChecked // finalArgs // {inherit outputs;};
config.package-func-args =
envChecked
// finalArgs
// {
inherit outputs;
inherit (config.public) name;
};
}
5 changes: 1 addition & 4 deletions modules/drv-parts/builtins-derivation/interface.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
builder = lib.mkOption {
type = t.oneOf [t.str t.path t.package];
};
name = lib.mkOption {
type = t.str;
};
system = lib.mkOption {
type = t.str;
};
};

in {
imports = [
../public/interface.nix
../public
../pkg-func/interface.nix
];

Expand Down
29 changes: 12 additions & 17 deletions modules/drv-parts/mkDerivation/implementation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.package-func-result ? name
then cfg.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
Expand All @@ -62,7 +50,14 @@ in {
config.package-func = lib.mkDefault config.deps.stdenv.mkDerivation;

# add mkDerivation specific derivation attributes
config.public = derivation;

config.package-func-args = envChecked // finalArgs // {inherit outputs;};
config.public = public;

config.package-func-args =
envChecked
// finalArgs
// {
inherit outputs;
inherit (config.public) version;
pname = config.public.name;
};
}
2 changes: 0 additions & 2 deletions modules/drv-parts/mkDerivation/interface.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@

# make-derivation args - without defaults
enableParallelChecking = optNullOrBool;
name = optNullOrStr;
pname = optNullOrStr;
realBuilder = optPackage;
requiredSystemFeatures = optListOfStr;
version = optNullOrStr;
Expand Down

0 comments on commit 939ad4d

Please sign in to comment.