1 { config, lib, pkgs, ... }:
2 let cfg = config.nix-plugins;
5 # Wrapper around nix to load extra-builtins.nix with nix-plugins.
6 nix-with-extra-builtins = pkgs.writeShellScriptBin "nix-with-extra-builtins" ''
8 --option plugin-files ${pkgs.nix-plugins}/lib/nix/plugins/libnix-extra-builtins.so \
9 --option extra-builtins-file ${cfg.extra-builtins} \
13 # Wrapper around pass to call it with exec in extra-builtins.nix.
14 # Unfortunately it can only load secrets which can be represented as a Nix string,
15 # hence without null-byte and such special characters.
16 nix-pass = pkgs.writeShellScriptBin "nix-pass" ''
19 trap "shred -u $f" EXIT
20 ${pkgs.pass}/bin/pass show "$1" >$f
21 nix-instantiate --eval -E "builtins.readFile $f"
28 ${pkgs.pass}/bin/pass show "$1" |
29 ${pkgs.gnused}/bin/sed \
30 -e 's:\n:\\n:g;s:\r:\\r:g;s:\t:\\t:g;s:":\\":g;1s:^:":;$s:$:":;'
33 # Wrapper around pass to call it with exec in extra-builtins.nix and put the output in a file.
34 # Needed for boot.initrd.network.ssh.host*Key.
35 nix-pass-to-file = pkgs.writeShellScriptBin "nix-pass-to-file" ''
38 ${pkgs.pass}/bin/pass show "$1" |
39 install -D -m 400 /dev/stdin "$2"
40 printf '%s\n' "$PWD/$2"
43 # Wrapper around git to call it with exec in extra-builtins.nix.
44 nix-git = pkgs.writeShellScriptBin "nix-git" ''
46 ${pkgs.git}/bin/git "$@"
50 options.nix-plugins = {
51 enable = lib.mkEnableOption "nix-plugins";
52 extra-builtins = lib.mkOption {
55 pass = path: exec [ "${nix-pass}/bin/nix-pass" path ];
56 pass-to-file = path: file: exec [ "${nix-pass-to-file}/bin/nix-pass-to-file" path file ];
57 git = dir: args: exec ([ "${nix-git}/bin/nix-git" (builtins.toPath dir) ] ++ args);
58 git-time = dir: path: exec [ "${nix-git}/bin/nix-git" (builtins.toPath dir) "log" "-1" "--format=%ct" "--" path ];
61 Content put in extra-builtins.nix for nix-plugins.
63 apply = lines: pkgs.writeText "extra-builtins.nix" (''
71 config = lib.mkIf cfg.enable {
74 plugin-files = ${pkgs.nix-plugins}/lib/nix/plugins/libnix-extra-builtins.so
75 extra-builtins-file = ${cfg.extra-builtins}