]> Git — Sourcephile - sourcephile-nix.git/blob - shell/modules/development/libraries/nix-plugins.nix
nix: set scheduler to none for ZFS and SSD
[sourcephile-nix.git] / shell / modules / development / libraries / nix-plugins.nix
1 # Extend the Nix interpreter to enable builtins.extraBuiltins,
2 # which provides an unsafe exec.
3 # Useful to get secrets from a local password-store.
4 { config, lib, pkgs, ... }:
5 let cfg = config.nix-plugins;
6 inherit (lib) types;
7
8 # Wrapper around nix to load extra-builtins.nix with nix-plugins.
9 nix-with-extra-builtins = pkgs.writeShellScriptBin "nix-with-extra-builtins" ''
10 ${pkgs.nix}/bin/nix \
11 --option plugin-files ${pkgs.nix-plugins}/lib/nix/plugins/libnix-extra-builtins.so \
12 --option extra-builtins-file ${cfg.extra-builtins} \
13 "$@"
14 '';
15
16 # Wrapper around pass to call it with exec in extra-builtins.nix.
17 # Unfortunately it can only load secrets which can be represented as a Nix string,
18 # hence without null-byte and such special characters.
19 nix-pass = pkgs.writeShellScriptBin "nix-pass" ''
20 set -e
21 f=$(mktemp)
22 trap "shred -u $f" EXIT
23 ${pkgs.pass}/bin/pass show "$1" >$f
24 nix-instantiate --eval -E "builtins.readFile $f"
25 '';
26 /*
27 nix-store --add $f
28 */
29 /*
30 set -o pipefail
31 ${pkgs.pass}/bin/pass show "$1" |
32 ${pkgs.gnused}/bin/sed \
33 -e 's:\n:\\n:g;s:\r:\\r:g;s:\t:\\t:g;s:":\\":g;1s:^:":;$s:$:":;'
34 */
35
36 # Wrapper around pass to call it with exec in extra-builtins.nix and put the output in a file.
37 # Needed for boot.initrd.network.ssh.host*Key.
38 nix-pass-to-file = pkgs.writeShellScriptBin "nix-pass-to-file" ''
39 set -e
40 set -o pipefail
41 ${pkgs.pass}/bin/pass show "$1" |
42 install -D -m 400 /dev/stdin "$2"
43 printf '%s\n' "$PWD/$2"
44 '';
45
46 # Wrapper around git to call it with exec in extra-builtins.nix.
47 nix-git = pkgs.writeShellScriptBin "nix-git" ''
48 cd "$1"; shift
49 ${pkgs.git}/bin/git "$@"
50 '';
51 in
52 {
53 options.nix-plugins = {
54 enable = lib.mkEnableOption "nix-plugins";
55 extra-builtins = lib.mkOption {
56 type = types.lines;
57 default = ''
58 pass = path: exec [ "${nix-pass}/bin/nix-pass" path ];
59 pass-to-file = path: file: exec [ "${nix-pass-to-file}/bin/nix-pass-to-file" path file ];
60 git = dir: args: exec ([ "${nix-git}/bin/nix-git" (builtins.toPath dir) ] ++ args);
61 git-time = dir: path: exec [ "${nix-git}/bin/nix-git" (builtins.toPath dir) "log" "-1" "--format=%ct" "--" path ];
62 '';
63 description = ''
64 Content put in extra-builtins.nix for nix-plugins.
65 '';
66 apply = lines: pkgs.writeText "extra-builtins.nix" (''
67 { exec, ... }:
68 {
69 '' + lines + ''
70 }
71 '');
72 };
73 };
74 config = lib.mkIf cfg.enable {
75 nix.enable = true;
76 nix.nixConf = ''
77 plugin-files = ${pkgs.nix-plugins}/lib/nix/plugins/libnix-extra-builtins.so
78 extra-builtins-file = ${cfg.extra-builtins}
79 '';
80 };
81 }