From 7f170376226fb52c8c10e86de56547f4ff4107fa Mon Sep 17 00:00:00 2001 From: DavHau Date: Thu, 1 Dec 2022 20:37:11 +0700 Subject: [PATCH] incorporate feedback from https://github.com/hercules-ci/flake-parts/issues/81#issuecomment-1333072961 --- examples/derivation/flake.nix | 4 ++-- examples/htop/flake.nix | 16 +++++++++++-- examples/htop/htop.nix | 22 +++++++++--------- examples/mkDerivation/flake.nix | 2 +- flake.nix | 11 ++++++--- modules/derivation-common/implementation.nix | 2 +- modules/derivation-common/interface.nix | 24 ++++++++++++++++++++ modules/derivation/implementation.nix | 3 ++- modules/drv-parts.nix | 11 +++++---- modules/mkDerivation/implementation.nix | 4 ++-- modules/mkDerivation/interface.nix | 3 +-- 11 files changed, 72 insertions(+), 30 deletions(-) diff --git a/examples/derivation/flake.nix b/examples/derivation/flake.nix index a3b1c69..4dcc1d3 100644 --- a/examples/derivation/flake.nix +++ b/examples/derivation/flake.nix @@ -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"]; diff --git a/examples/htop/flake.nix b/examples/htop/flake.nix index a2de65b..c658c4f 100644 --- a/examples/htop/flake.nix +++ b/examples/htop/flake.nix @@ -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 + ; + }; + }; }; }; } diff --git a/examples/htop/htop.nix b/examples/htop/htop.nix index 16b8e88..07aed57 100644 --- a/examples/htop/htop.nix +++ b/examples/htop/htop.nix @@ -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 { @@ -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" ] @@ -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; { diff --git a/examples/mkDerivation/flake.nix b/examples/mkDerivation/flake.nix index d0f5c50..6242948 100644 --- a/examples/mkDerivation/flake.nix +++ b/examples/mkDerivation/flake.nix @@ -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]; diff --git a/flake.nix b/flake.nix index 2d2fe7d..a0f7143 100644 --- a/flake.nix +++ b/flake.nix @@ -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; diff --git a/modules/derivation-common/implementation.nix b/modules/derivation-common/implementation.nix index ff43e39..7a9d722 100644 --- a/modules/derivation-common/implementation.nix +++ b/modules/derivation-common/implementation.nix @@ -1,4 +1,4 @@ -{config, lib, pkgs, ...}: let +{config, lib, ...}: let l = lib // builtins; t = l.types; in { diff --git a/modules/derivation-common/interface.nix b/modules/derivation-common/interface.nix index 7c15881..effa825 100644 --- a/modules/derivation-common/interface.nix +++ b/modules/derivation-common/interface.nix @@ -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); diff --git a/modules/derivation/implementation.nix b/modules/derivation/implementation.nix index 629c20f..c5a357e 100644 --- a/modules/derivation/implementation.nix +++ b/modules/derivation/implementation.nix @@ -1,4 +1,4 @@ -{config, lib, pkgs, ...}: let +{config, lib, ...}: let l = lib // builtins; t = l.types; @@ -8,6 +8,7 @@ "_module" # this module's options which should not end up in the drv "derivation" + "deps" "env" "drvPath" "type" diff --git a/modules/drv-parts.nix b/modules/drv-parts.nix index 26e4d5d..e6ae200 100644 --- a/modules/drv-parts.nix +++ b/modules/drv-parts.nix @@ -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; }; } ); @@ -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; }; } diff --git a/modules/mkDerivation/implementation.nix b/modules/mkDerivation/implementation.nix index ffc8705..10fbc82 100644 --- a/modules/mkDerivation/implementation.nix +++ b/modules/mkDerivation/implementation.nix @@ -1,12 +1,11 @@ { config, lib, - pkgs, + stdenv, ... }: let l = lib // builtins; t = l.types; - stdenv = pkgs.stdenv; # args that should not be passed to mkDerivation argsIgnore = [ @@ -14,6 +13,7 @@ "_module" # this module's options which should not end up in the drv "derivation" + "deps" "env" "drvPath" "type" diff --git a/modules/mkDerivation/interface.nix b/modules/mkDerivation/interface.nix index 22dab6a..b55f075 100644 --- a/modules/mkDerivation/interface.nix +++ b/modules/mkDerivation/interface.nix @@ -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;