Skip to content

Commit

Permalink
incorporate feedback from hercules-ci/flake-parts#81 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavHau committed Dec 1, 2022
1 parent 121a2ba commit 7f17037
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 30 deletions.
4 changes: 2 additions & 2 deletions examples/derivation/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

perSystem = {config, pkgs, system, ...}: {
checks = config.packages;
pkgs.hello = {
drvs.hello = {

# select mkDerivation as a backend for this package
imports = [drv-parts.modules.derivation];

# # set options
# set options
name = "test";
builder = "/bin/sh";
args = ["-c" "echo $name > $out"];
Expand Down
16 changes: 14 additions & 2 deletions examples/htop/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@
# enable the drv-parts plugin for flake-parts
imports = [drv-parts.flakeModule];

perSystem = {config, lib, pkgs, ...}: {
perSystem = {config, lib, pkgs, ...}: {
checks = config.packages;
pkgs.htop = import ./htop.nix;
drvs.htop = {
imports = [./htop.nix];
deps = {
inherit (pkgs)
autoreconfHook
fetchFromGitHub
IOKit
lm_sensors
ncurses
systemd
;
};
};
};
};
}
22 changes: 11 additions & 11 deletions examples/htop/htop.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{config, inputs', lib, pkgs, ...}: let
stdenv = pkgs.stdenv;
{config, lib, stdenv, drv-backends, ...}: let
deps = config.deps;
in {

# select mkDerivation as a backend for this package
imports = builtins.trace inputs'.drv-parts [inputs'.drv-parts.modules.mkDerivation];
imports = [drv-backends.mkDerivation];

options = {
sensorsSupport = lib.mkOption {
Expand All @@ -21,19 +21,19 @@ in {
pname = "htop";
version = "3.2.1";

src = pkgs.fetchFromGitHub {
src = deps.fetchFromGitHub {
owner = "htop-dev";
repo = config.pname;
rev = config.version;
sha256 = "sha256-MwtsvdPHcUdegsYj9NGyded5XJQxXri1IM1j4gef1Xk=";
};

nativeBuildInputs = [ pkgs.autoreconfHook ];
nativeBuildInputs = [ deps.autoreconfHook ];

buildInputs = [ pkgs.ncurses ]
++ lib.optional stdenv.isDarwin pkgs.IOKit
++ lib.optional config.sensorsSupport pkgs.lm_sensors
++ lib.optional config.systemdSupport pkgs.systemd
buildInputs = [ deps.ncurses ]
++ lib.optional stdenv.isDarwin deps.IOKit
++ lib.optional config.sensorsSupport deps.lm_sensors
++ lib.optional config.systemdSupport deps.systemd
;

configureFlags = [ "--enable-unicode" "--sysconfdir=/etc" ]
Expand All @@ -45,8 +45,8 @@ in {
optionalPatch = pred: so: lib.optionalString pred "patchelf --add-needed ${so} $out/bin/htop";
in
''
${optionalPatch config.sensorsSupport "${pkgs.lm_sensors}/lib/libsensors.so"}
${optionalPatch config.systemdSupport "${pkgs.systemd}/lib/libsystemd.so"}
${optionalPatch config.sensorsSupport "${deps.lm_sensors}/lib/libsensors.so"}
${optionalPatch config.systemdSupport "${deps.systemd}/lib/libsystemd.so"}
'';

meta = with lib; {
Expand Down
2 changes: 1 addition & 1 deletion examples/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;
pkgs.hello = {
drvs.hello = {

# select mkDerivation as a backend for this package
imports = [drv-parts.modules.mkDerivation];
Expand Down
11 changes: 8 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
systems = ["x86_64-linux"];

flake = {
flakeModule = self.nixosModules.drv-parts;
modules = self.nixosModules;
nixosModules = {
flakeModule = self.modules.drv-parts;
drv-backends = {
inherit (self.modules)
derivation
mkDerivation
;
};
modules = {
# import one of these to pick the backend for your derivation
# TODO: add more backends like for ex.: buildPythonPackage, etc.
derivation = ./modules/derivation;
Expand Down
2 changes: 1 addition & 1 deletion modules/derivation-common/implementation.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{config, lib, pkgs, ...}: let
{config, lib, ...}: let
l = lib // builtins;
t = l.types;
in {
Expand Down
24 changes: 24 additions & 0 deletions modules/derivation-common/interface.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ in {
type = t.package;
};

/*
This allows defining drvs in an encapsulated manner, while maintaining
the capability to depend on external attributes
*/
deps = l.mkOption {
description = ''
All dependencies of the package. This option should be set by the "outer world" and can be used to inherit attributes from `pkgs` or `inputs` etc.
By separating the task of retrieving things from the outside world, it is ensured that the dependencies are overridable.
Nothing will stop users from adding `pkgs` itself as a dependency, but this will make it very hard for the user of the package to override any dependencies, because they'd have to figure out a way to insert their changes into the Nixpkgs fixpoint. By adding specific attributes to `deps` instead, the user has a realistic chance of overriding those dependencies.
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.lazyAttrsOf t.raw;
default = {};
example = lib.literalExpression ''
{
inherit (pkgs) stdenv;
inherit (pkgs.haskellPackages) pandoc;
nix = inputs'.nix.packages.default;
}
'';
};

# basic arguments
args = lib.mkOption {
type = t.nullOr (t.listOf t.str);
Expand Down
3 changes: 2 additions & 1 deletion modules/derivation/implementation.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{config, lib, pkgs, ...}: let
{config, lib, ...}: let
l = lib // builtins;
t = l.types;

Expand All @@ -8,6 +8,7 @@
"_module"
# this module's options which should not end up in the drv
"derivation"
"deps"
"env"
"drvPath"
"type"
Expand Down
11 changes: 6 additions & 5 deletions modules/drv-parts.nix
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{ config, lib, flake-parts-lib, ... }:
{ config, lib, flake-parts-lib, inputs, ... }:
let
l = lib // builtins;
t = l.types;
in {
options.perSystem =
flake-parts-lib.mkPerSystemOption ({pkgs, inputs', ...}: {
options.pkgs = l.mkOption {
options.drvs = l.mkOption {
type = t.lazyAttrsOf (
t.submoduleWith {
modules = [./derivation-common];
specialArgs = {
inherit pkgs;
# inherit pkgs;
inherit (pkgs) stdenv;
nixpkgsConfig = pkgs.config;
inherit inputs';
inherit (inputs.drv-parts) drv-backends;
};
}
);
Expand All @@ -32,7 +33,7 @@ in {
This exposes the `.derivation` attribute (the actual derivation) of each
defined `pkgs.xxx` under the flake output `packages`.
*/
config.packages = l.mapAttrs (name: pkg: pkg.derivation) config.pkgs;
config.packages = l.mapAttrs (name: pkg: pkg.derivation) config.drvs;
};

}
4 changes: 2 additions & 2 deletions modules/mkDerivation/implementation.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
config,
lib,
pkgs,
stdenv,
...
}: let
l = lib // builtins;
t = l.types;
stdenv = pkgs.stdenv;

# args that should not be passed to mkDerivation
argsIgnore = [
# attrs introduced by module system
"_module"
# this module's options which should not end up in the drv
"derivation"
"deps"
"env"
"drvPath"
"type"
Expand Down
3 changes: 1 addition & 2 deletions modules/mkDerivation/interface.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{config, lib, pkgs, nixpkgsConfig, ...}: let
{config, lib, stdenv, nixpkgsConfig, ...}: let
l = lib // builtins;
t = l.types;
stdenv = pkgs.stdenv;
optNullOrStr = l.mkOption {
type = t.nullOr t.str;
default = null;
Expand Down

0 comments on commit 7f17037

Please sign in to comment.