Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/no-flake/derivation/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
system = builtins.currentSystem;
};
in
drv-parts.lib.derivationFromModules hello
drv-parts.lib.derivationFromModules {} hello
2 changes: 1 addition & 1 deletion examples/no-flake/hello-from-nixpkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
stdenv = pkgs.stdenv;
};

hello = drv-parts.lib.derivationFromModules [
hello = drv-parts.lib.derivationFromModules {} [
helloDefaultNix
helloDeps
];
Expand Down
2 changes: 1 addition & 1 deletion examples/no-flake/mkDerivation/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
};
};
in
drv-parts.lib.derivationFromModules hello
drv-parts.lib.derivationFromModules {} hello
3 changes: 2 additions & 1 deletion lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
let
l = lib // builtins;

derivationFromModules = modules: let
derivationFromModules = dependencySets: modules: let
drv = lib.evalModules {
modules = if l.isList modules then modules else [modules];
specialArgs = {inherit dependencySets;};
};
in
drv.config.final.derivation;
Expand Down
48 changes: 21 additions & 27 deletions lib/makeModule.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,27 @@ defaultNix: let
# generated nixos options for all flags
flagOptions = makeFlagOptions flagArgs;

# throws error listing missing deps.xxx entries
throwMissingDepsError = missingDepNames: throw ''
You are trying to generate a module from a legacy default.nix file
located under ${defaultNix},
but the `deps` option is not populated with all required dependencies.
The following dependencies are missing:
- ${l.concatStringsSep "\n - " missingDepNames}
'';

# Ensure that all required default.nix dependencies are passed via `deps`.
# This is a bit hacky. It would be nicer if we could define `deps.{foo}`
# as an individual option, but `deps` is already defined as `coercedTo`
# which does not support nested options.
ensureDepsPopulated = deps: let
missingDeps = l.filterAttrs (depName: _: ! deps ? ${depName}) depArgs;
missingDepNames = l.attrNames missingDeps;
in
if missingDeps == {}
then deps
else throwMissingDepsError missingDepNames;

# override func that exposes mkDerivation arguments
passthruMkDrvArgs = oldArgs: {passthru.__mkDrvArgs = oldArgs;};

getMkDrvArgs = drv: (drv.overrideAttrs passthruMkDrvArgs).__mkDrvArgs;

mkDepOpt = depName: _: l.mkOption {
description = "Specify a package for the dependency ${depName}.";
type = t.raw;
};

in {config, options, ...}: {

imports = [../modules/mkDerivation/interface.nix];

options.flags = flagOptions;
options.deps = l.mapAttrs mkDepOpt depArgs;

config = let

# raises errors if a dependency is missing from `config.deps`
ensuredDeps = ensureDepsPopulated config.deps;

pickFlag = flagName: _: config.flags.${flagName};
pickDep = depName: _: ensuredDeps.${depName};
pickDep = depName: _: config.deps.${depName};
flagArgs' = l.mapAttrs pickFlag flagArgs;
depArgs' = l.mapAttrs pickDep depArgs;

Expand Down Expand Up @@ -123,12 +105,24 @@ in {config, options, ...}: {
config.stdenv = config.stdenv;
};

finalDerivation = drvPartsLib.derivationFromModules [finalDrvModule];
finalDerivation = drvPartsLib.derivationFromModules {} [finalDrvModule];

/*
Populate deps with some defaults.
`lib` should be taken from the current module.
`stdenv` should be taken from `config.stdenv`.
*/
deps' = {
inherit lib;
inherit (config) stdenv;
};
deps'' = l.intersectAttrs depArgs deps';
deps = l.mapAttrs (_: dep: l.mkDefault dep) deps'';

in

{
deps.lib = lib;
deps = deps;
final.derivation = finalDerivation;
};
}
13 changes: 6 additions & 7 deletions modules/derivation-common/interface.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{config, lib, dependencySets, ...}: let
l = lib // builtins;
t = l.types;
callDeps = func: func dependencySets;
optNullOrBool = l.mkOption {
type = t.nullOr t.bool;
default = null;
Expand Down Expand Up @@ -116,12 +115,12 @@

So deps should be specific, but not overly specific. For instance, the caller shouldn't have to know the version of a dependency in order to override it. The name should suffice. (e.g. `nix = nixVersions.nix_2_12` instead of `inherit (nixVersions) nix_2_12`.
'';
type =
t.coercedTo
(t.functionTo (t.lazyAttrsOf t.raw))
callDeps
(t.lazyAttrsOf t.raw);
default = {};
type = t.submoduleWith {
# TODO: This could be made stricter by removing the freeformType
# Maybe add option `strictDeps = true/false` ? ;P
modules = [{freeformType = t.lazyAttrsOf t.raw;}];
specialArgs = dependencySets;
};
example = lib.literalExpression ''
{pkgs, inputs', ...}: {
inherit (pkgs) stdenv;
Expand Down
4 changes: 2 additions & 2 deletions modules/drv-parts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ in {
modules = [./derivation-common];
specialArgs = {
inherit (inputs.drv-parts) drv-backends;
inherit (config) dependencySets;
inherit (config.drv-parts) dependencySets;
};
}
);
Expand Down Expand Up @@ -66,7 +66,7 @@ in {
'';
};

dependencySets = l.mkOption {
drv-parts.dependencySets = l.mkOption {
type = t.lazyAttrsOf t.raw;
default = {
inherit pkgs inputs';
Expand Down