Skip to content

Commit

Permalink
minor fixes + add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DavHau committed Nov 30, 2022
1 parent a2de655 commit cc4cf9f
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 35 deletions.
35 changes: 35 additions & 0 deletions examples/derivation/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "nixpkgs/nixos-unstable";
drv-parts.url = "nixpkgs";
};

outputs = {
self,
flake-parts,
drv-parts,
...
}:
flake-parts.lib.mkFlake {inherit self;} {
systems = ["x86_64-linux"];

# enable the drv-parts plugin for flake-parts
imports = [drv-parts.modules.drv-parts];

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

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

# # set options
name = "test";
builder = "/bin/sh";
args = ["-c" "echo $name > $out"];
system = system;
};
};
};
}
25 changes: 25 additions & 0 deletions examples/htop/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "nixpkgs/nixos-unstable";
drv-parts.url = "nixpkgs";
};

outputs = {
self,
flake-parts,
drv-parts,
...
}:
flake-parts.lib.mkFlake {inherit self;} {
systems = ["x86_64-linux"];

# enable the drv-parts plugin for flake-parts
imports = [drv-parts.flakeModule];

perSystem = {config, lib, pkgs, ...}: {
checks = config.packages;
pkgs.htop = import ./htop.nix;
};
};
}
61 changes: 61 additions & 0 deletions examples/htop/htop.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{config, inputs', lib, pkgs, ...}: let
stdenv = pkgs.stdenv;
in {

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

options = {
sensorsSupport = lib.mkOption {
type = lib.types.bool;
default = stdenv.isLinux;
};
systemdSupport = lib.mkOption {
type = lib.types.bool;
default = stdenv.isLinux;
};
};

config = {
# set options
pname = "htop";
version = "3.2.1";

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

nativeBuildInputs = [ pkgs.autoreconfHook ];

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

configureFlags = [ "--enable-unicode" "--sysconfdir=/etc" ]
++ lib.optional config.sensorsSupport "--with-sensors"
;

postFixup =
let
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"}
'';

meta = with lib; {
description = "An interactive process viewer";
homepage = "https://htop.dev";
license = licenses.gpl2Only;
platforms = platforms.all;
maintainers = with maintainers; [ rob relrod SuperSandro2000 ];
changelog = "https://github.com/htop-dev/htop/blob/${version}/ChangeLog";
};
};
}
36 changes: 36 additions & 0 deletions examples/mkDerivation/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "nixpkgs/nixos-unstable";
drv-parts.url = "nixpkgs";
};

outputs = {
self,
flake-parts,
drv-parts,
...
}:
flake-parts.lib.mkFlake {inherit self;} {
systems = ["x86_64-linux"];

# enable the drv-parts plugin for flake-parts
imports = [drv-parts.modules.drv-parts];

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

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

# set options
name = "hello";
src = pkgs.fetchurl {
url = "mirror://gnu/hello/hello-2.12.1.tar.gz";
sha256 = "sha256-jZkUKv2SV28wsM18tCqNxoCZmLxdYH2Idh9RLibH2yA=";
};
};
};
};
}
41 changes: 32 additions & 9 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
description = ''
construct derivations using the nixos-module system
'';

inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "nixpkgs/nixos-unstable";
Expand All @@ -7,21 +11,40 @@
outputs = {
self,
flake-parts,
nixpkgs,
...
}:
flake-parts.lib.mkFlake {inherit self;} {
systems = ["x86_64-linux"];
imports = [./modules/drv-parts.nix];

perSystem = {config, pkgs, ...}: {
pkgs.hello = {
name = "hello";
src = pkgs.fetchurl {
url = "mirror://gnu/hello/hello-2.12.1.tar.gz";
sha256 = "sha256-jZkUKv2SV28wsM18tCqNxoCZmLxdYH2Idh9RLibH2yA=";
};
passthru.yolo = "lol";
flake = {
flakeModule = self.nixosModules.drv-parts;
modules = self.nixosModules;
nixosModules = {

This comment has been minimized.

Copy link
@roberth

roberth Dec 1, 2022

Contributor

I think nixosModules items are supposed to be importable into the top level NixOS configuration.

These are definitely modules, but nixos*, I don't think that's right for them.

I like modules.{package,nixos,flake-parts,nix-darwin}, but that's unconventional and maybe over-engineering it. It does define convention that's more accomodating for all module system application, that's even flake check-able if Nix adopts this as the standard.

This comment has been minimized.

Copy link
@DavHau

DavHau Dec 3, 2022

Author Owner

Good point. In the mean time I changed it to modules. Hopefully a standard will emerge at some point. Is there an open issue for this on nix yet?

This comment has been minimized.

Copy link
@roberth

roberth Dec 4, 2022

Contributor
# import one of these to pick the backend for your derivation
# TODO: add more backends like for ex.: buildPythonPackage, etc.
derivation = ./modules/derivation;
mkDerivation = ./modules/mkDerivation;

# the main module creatig the `.pkgs.[...]` option for flake-parts
drv-parts = ./modules/drv-parts.nix;

# the base derivation type used by the drv-parts module
derivation-common = ./modules/derivation-common;
};
};

perSystem = {system, pkgs, ...}: {
packages.tests-examples = pkgs.writeShellScriptBin "tests-examples" ''
set -eu -o pipefail
for example in $(find ./examples); do
echo "testing example $example"
nix flake check "$example" -L \
--show-trace \
--no-write-lock-file \
--override-input drv-parts ${self}
done
'';
};
};
}
3 changes: 0 additions & 3 deletions modules/derivation-common/interface.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ in {
type = t.attrsOf (t.oneOf [t.bool t.int t.str t.path t.package]);
default = {};
};
name = lib.mkOption {
type = t.str;
};
outputs = lib.mkOption {
type = t.listOf t.str;
default = ["out"];
Expand Down
60 changes: 41 additions & 19 deletions modules/derivation/implementation.nix
Original file line number Diff line number Diff line change
@@ -1,42 +1,64 @@
{config, lib, pkgs, ...}: let
l = lib // builtins;
t = l.types;
argsFiltered = l.removeAttrs config [

# args that should not be passed to derivatio
argsIgnore = [
# attrs introduced by module system
"_module"
# this module's custom options
# this module's options which should not end up in the drv
"derivation"
"env"
# attributes that should not be passed if null
"args"
"outputHash"
"drvPath"
"type"
];

# attributes that should not be passed if null
argsIgnoreIfNull = [
"outputHash"
];

/*
Filters out args which potentially must be removed because they are null.
Later, the ones which are not null will be added back via `argsMaybeIgnored`
*/
argsFiltered = l.removeAttrs config (argsIgnore ++ argsIgnoreIfNull);

# esure that none of the env variables collides with the top-level options
envCollisionError = key: ''
The environment variable declared via env.${key} collides with option ${key}.
Specify the option instead, or rename the environment variable.
'';
envChecked =
l.mapAttrs
(key: val:
if config ? ${key}
then throw (envCollisionError key)
else val)
config.env;

# generates error message for env variable collision
envCollisionError = key: ''
The environment variable declared via env.${key} collides with option ${key}.
Specify the option instead, or rename the environment variable.
'';

# returns empty attrset if value == null
omitIfNull = argName:
if config.${argName} == null
then {}
else {${argName} = config.${argName};};

# only contains args for which value != null
argsMaybeIgnored =
l.foldl
(all: argName: all // (omitIfNull argName))
{}
argsIgnoreIfNull;

# all args that are massed directly to mkDerivation
args =
envChecked
// argsFiltered
// (
if config.args == null
then {}
else {inherit (config) args;}
)
// (
if config.outputHash == null
then {}
else {inherit (config) outputHash;}
);
// argsMaybeIgnored
;

in {
config.derivation = derivation args;
}
5 changes: 4 additions & 1 deletion modules/derivation/interface.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
t = l.types;
in {
imports = [
../derivation-commmon
../derivation-common
];
options = {
# basic arguments
builder = lib.mkOption {
type = t.oneOf [t.str t.path t.package];
};
name = lib.mkOption {
type = t.str;
};
__contentAddressed = lib.mkOption {
type = t.bool;
default = false;
Expand Down
5 changes: 3 additions & 2 deletions modules/drv-parts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ let
t = l.types;
in {
options.perSystem =
flake-parts-lib.mkPerSystemOption ({pkgs, ...}: {
flake-parts-lib.mkPerSystemOption ({pkgs, inputs', ...}: {
options.pkgs = l.mkOption {
type = t.lazyAttrsOf (
t.submoduleWith {
modules = [./mkDerivation];
modules = [./derivation-common];
specialArgs = {
inherit pkgs;
nixpkgsConfig = pkgs.config;
inherit inputs';
};
}
);
Expand Down
2 changes: 1 addition & 1 deletion modules/mkDerivation/implementation.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
config,
nixpkgsConfig,
lib,
pkgs,
...
Expand All @@ -26,6 +25,7 @@
"args"
"outputHash"
"realBuilder"
"name"
"pname"
"version"
"__contentAddressed"
Expand Down
4 changes: 4 additions & 0 deletions modules/mkDerivation/interface.nix
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ in {

# make-derivation args - without defaults
enableParallelChecking = optBoolWithDefault true;
name = l.mkOption {
type = t.nullOr t.str;
default = null;
};
pname = l.mkOption {
type = t.nullOr t.str;
default = null;
Expand Down

0 comments on commit cc4cf9f

Please sign in to comment.