prepare fs.generated.script users to not assume a shell

This commit is contained in:
Colin 2023-07-08 10:34:46 +00:00
parent 3ce2716fbe
commit 558b35fee0
10 changed files with 48 additions and 28 deletions

View File

@ -1,10 +1,16 @@
{ config, sane-lib, ... }: { config, pkgs, sane-lib, ... }:
let
init-keyring = pkgs.static-nix-shell.mkBash {
pname = "init-keyring";
src = ./.;
};
in
{ {
sane.user.persist.private = [ ".local/share/keyrings" ]; sane.user.persist.private = [ ".local/share/keyrings" ];
sane.user.fs."private/.local/share/keyrings/default" = { sane.user.fs."private/.local/share/keyrings/default" = {
generated.script.script = builtins.readFile ./init-keyring; generated.script.script = "${init-keyring}/bin/init-keyring";
# TODO: is this `wantedBy` needed? can we inherit it? # TODO: is this `wantedBy` needed? can we inherit it?
wantedBy = [ config.sane.fs."/home/colin/private".unit ]; wantedBy = [ config.sane.fs."/home/colin/private".unit ];
wantedBeforeBy = [ ]; # don't created this as part of `multi-user.target` wantedBeforeBy = [ ]; # don't created this as part of `multi-user.target`

View File

@ -1,4 +1,5 @@
#!/bin/sh #!/usr/bin/env nix-shell
#!nix-shell -i bash
# initializes the default libsecret keyring (used by gnome-keyring) if not already initialized. # initializes the default libsecret keyring (used by gnome-keyring) if not already initialized.
# this initializes it to be plaintext/unencrypted. # this initializes it to be plaintext/unencrypted.

View File

@ -2,7 +2,7 @@
{ {
imports = [ imports = [
./derived-secrets.nix ./derived-secrets
./gui ./gui
./hardware ./hardware
./hostnames.nix ./hostnames.nix

View File

@ -1,8 +1,14 @@
{ config, lib, ... }: { config, lib, pkgs, ... }:
let let
inherit (builtins) toString; inherit (builtins) toString;
inherit (lib) mapAttrs mkOption types; inherit (lib) mapAttrs mkOption types;
hash-path-with-salt = pkgs.static-nix-shell.mkBash {
pname = "hash-path-with-salt";
src = ./.;
};
cfg = config.sane.derived-secrets; cfg = config.sane.derived-secrets;
secret = types.submodule { secret = types.submodule {
options = { options = {
@ -31,15 +37,7 @@ in
config = { config = {
sane.fs = mapAttrs (path: c: { sane.fs = mapAttrs (path: c: {
generated.script.script = '' generated.script.script = ''${hash-path-with-salt}/bin/hash-path-with-salt "$@"'';
echo "$1" | cat /dev/stdin /etc/ssh/host_keys/ssh_host_ed25519_key \
| sha512sum \
| cut -c 1-${toString (c.len * 2)} \
| tr a-z A-Z \
| basenc -d --base16 \
| basenc --${c.encoding} \
> "$1"
'';
generated.script.scriptArgs = [ path ]; generated.script.scriptArgs = [ path ];
generated.acl.mode = "0600"; generated.acl.mode = "0600";
}) cfg; }) cfg;

View File

@ -0,0 +1,9 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash
echo "$1" | cat /dev/stdin /etc/ssh/host_keys/ssh_host_ed25519_key \
| sha512sum \
| cut -c 1-${toString (c.len * 2)} \
| tr a-z A-Z \
| basenc -d --base16 \
| basenc --${c.encoding} \
> "$1"

View File

@ -15,11 +15,8 @@ in
sane.fs."/var/lib/bluetooth".generated.acl.mode = "0700"; sane.fs."/var/lib/bluetooth".generated.acl.mode = "0700";
sane.fs."/var/lib/bluetooth/.secrets.stamp" = { sane.fs."/var/lib/bluetooth/.secrets.stamp" = {
wantedBeforeBy = [ "bluetooth.service" ]; wantedBeforeBy = [ "bluetooth.service" ];
generated.script.script = '' generated.script.script = ''${install-bluetooth}/bin/install-bluetooth "$@"'';
${install-bluetooth}/bin/install-bluetooth $@ generated.script.scriptArgs = [ "/run/secrets/bt" "" "/var/lib/bluetooth/.secrets.stamp" ];
touch "/var/lib/bluetooth/.secrets.stamp"
'';
generated.script.scriptArgs = [ "/run/secrets/bt" ];
}; };
}; };
} }

View File

@ -12,6 +12,7 @@
srcdir="$1" srcdir="$1"
destdir="$2" destdir="$2"
stamp="$3"
if [ "x$destdir" = "x" ] if [ "x$destdir" = "x" ]
then then
@ -36,3 +37,8 @@ do
touch "$condir/attributes" touch "$condir/attributes"
fi fi
done done
if [ "x$stamp" != "x" ]
then
touch "$stamp"
fi

View File

@ -10,6 +10,7 @@
src_dir="$1" src_dir="$1"
dest_dir="$2" dest_dir="$2"
stamp="$3"
for f in $(ls "$src_dir") for f in $(ls "$src_dir")
do do
if [ -f "$src_dir/$f" ]; then if [ -f "$src_dir/$f" ]; then
@ -21,3 +22,4 @@ do
chmod 600 "$dest_dir/$ssid.psk" chmod 600 "$dest_dir/$ssid.psk"
fi fi
done done
touch "$stamp"

View File

@ -12,11 +12,8 @@ in
sane.fs."/var/lib/iwd/.secrets.psk.stamp" = { sane.fs."/var/lib/iwd/.secrets.psk.stamp" = {
wantedBeforeBy = [ "iwd.service" ]; wantedBeforeBy = [ "iwd.service" ];
generated.acl.mode = "0600"; generated.acl.mode = "0600";
generated.script.script = '' generated.script.script = ''${install-iwd}/bin/install-iwd "$@"'';
${install-iwd}/bin/install-iwd $@ generated.script.scriptArgs = [ "/run/secrets/net" "/var/lib/iwd" "/var/lib/iwd/.secrets.psk.stamp" ];
touch "/var/lib/iwd/.secrets.psk.stamp"
'';
generated.script.scriptArgs = [ "/run/secrets/net" "/var/lib/iwd" ];
}; };
}; };
} }

View File

@ -41,8 +41,8 @@ lib.mkIf config.sane.persist.enable
# let sane.fs know how to initialize the gocryptfs store, # let sane.fs know how to initialize the gocryptfs store,
# and that it MUST do so # and that it MUST do so
sane.fs."${underlying}/gocryptfs.conf".generated = { sane.fs."${underlying}/gocryptfs.conf".generated = let
script.script = '' script = pkgs.writeShellScript "init-gocryptfs-store" ''
backing="$1" backing="$1"
passfile="$2" passfile="$2"
# clear the backing store # clear the backing store
@ -50,16 +50,20 @@ lib.mkIf config.sane.persist.enable
rm -rf "''${backing:?}"/* rm -rf "''${backing:?}"/*
${pkgs.gocryptfs}/bin/gocryptfs -quiet -passfile "$passfile" -init "$backing" ${pkgs.gocryptfs}/bin/gocryptfs -quiet -passfile "$passfile" -init "$backing"
''; '';
in {
script.script = ''${script} "$@"'';
script.scriptArgs = [ underlying key ]; script.scriptArgs = [ underlying key ];
# we need the key in order to initialize the store # we need the key in order to initialize the store
depends = [ config.sane.fs."${key}".unit ]; depends = [ config.sane.fs."${key}".unit ];
}; };
# let sane.fs know how to generate the key for gocryptfs # let sane.fs know how to generate the key for gocryptfs
sane.fs."${key}".generated = { sane.fs."${key}".generated = let
script.script = '' script = pkgs.writeShellScript "gen-random-gocryptfs-key" ''
dd if=/dev/random bs=128 count=1 | base64 --wrap=0 > "$1" dd if=/dev/random bs=128 count=1 | base64 --wrap=0 > "$1"
''; '';
in {
script.script = ''${script} "$@"'';
script.scriptArgs = [ key ]; script.scriptArgs = [ key ];
# no need for anyone else to be able to read the key # no need for anyone else to be able to read the key
acl.mode = "0400"; acl.mode = "0400";