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;
8 # Wrapper around nix to load extra-builtins.nix with nix-plugins.
9 nix-with-extra-builtins = pkgs.writeShellScriptBin "nix-with-extra-builtins" ''
11 --option plugin-files ${pkgs.nix-plugins}/lib/nix/plugins/libnix-extra-builtins.so \
12 --option extra-builtins-file ${cfg.extra-builtins} \
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" ''
22 trap "shred -u $f" EXIT
23 ${pkgs.pass}/bin/pass show "$1" >$f
24 nix-instantiate --eval -E "builtins.readFile $f"
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:$:":;'
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" ''
41 ${pkgs.pass}/bin/pass show "$1" |
42 install -D -m 400 /dev/stdin "$2"
43 printf '%s\n' "$PWD/$2"
46 # Wrapper around git to call it with exec in extra-builtins.nix.
47 nix-git = pkgs.writeShellScriptBin "nix-git" ''
49 ${pkgs.git}/bin/git "$@"
53 options.nix-plugins = {
54 enable = lib.mkEnableOption "nix-plugins";
55 extra-builtins = lib.mkOption {
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 ];
64 Content put in extra-builtins.nix for nix-plugins.
66 apply = lines: pkgs.writeText "extra-builtins.nix" (''
74 config = lib.mkIf cfg.enable {
77 plugin-files = ${pkgs.nix-plugins}/lib/nix/plugins/libnix-extra-builtins.so
78 extra-builtins-file = ${cfg.extra-builtins}