[flake] Extract devShell scripts

Also start using writeShellApplication.
This commit is contained in:
Malte Tammena 2022-04-18 18:12:06 +02:00
parent 64b5b51b01
commit 73ffca6989
5 changed files with 173 additions and 62 deletions

119
flake.nix
View file

@ -269,77 +269,72 @@
outputsBuilder = channels: {
devShell = channels.nixpkgs.mkShell {
packages = let
personalCache = pkgs.writeShellApplication {
name = "personal-cache";
runtimeInputs = with pkgs; [
coreutils
nix
];
text = builtins.readFile ./scripts/personal-cache.sh;
};
rebuild = pkgs.writeShellApplication {
name = "rebuild";
runtimeInputs = with pkgs; [
coreutils
git
personalCache
nixos-rebuild
];
text = builtins.readFile ./scripts/rebuild.sh;
};
allHosts = pkgs.writeShellApplication {
name = "all-hosts";
runtimeInputs = with pkgs; [
jq
nix
];
text = ''
nix eval --json .#nixosConfigurations --apply builtins.attrNames 2>/dev/null | jq -r .[]
'';
};
option = pkgs.writeShellApplication {
name = "option";
runtimeInputs = with pkgs; [
coreutils
nix
];
text = builtins.readFile ./scripts/option.sh;
};
testConfig = pkgs.writeShellApplication {
name = "test-config";
runtimeInputs = with pkgs; [
coreutils
nix
allHosts
nixos-rebuild
bat
];
text = builtins.readFile ./scripts/test-config.sh;
};
date = "${pkgs.coreutils}/bin/date";
git = "${pkgs.git}/bin/git";
nixos-rebuild = "${pkgs.nixos-rebuild}/bin/nixos-rebuild";
sops = "${pkgs.sops}/bin/sops";
bat = "${pkgs.bat}/bin/bat";
echo = "${pkgs.coreutils}/bin/echo";
personalCache = "http://elysia-clarki:5000";
pingPersonalCache = "${pkgs.nix}/bin/nix store ping --store ${personalCache} --option connect-timeout 1 --option download-attempt 1 2>/dev/null";
in
with pkgs; [
(writeScriptBin "rebuild" ''
#!${stdenv.shell}
if [ -z $1 ]; then
echo "Need 'switch|boot|...'"
exit 1
fi
# Check if personal cache is online
if ${pingPersonalCache}; then
extraSubstituters="--option extra-substituters ${personalCache}"
else
${echo} "Cache ${personalCache} is unreachable!"
fi
today=`${date} +%Y-%m-%d`
branch=`${git} branch --show-current`
rev=`${git} log -1 --format=%h`
dirty=`${git} diff --quiet || echo '~'`
NIXOS_LABEL_VERSION=$today-$branch-$rev$dirty
sudo ${nixos-rebuild} $1 --flake . $extraSubstituters
'')
(writeScriptBin "all-hosts" ''
nix eval --json .#nixosConfigurations --apply builtins.attrNames 2>/dev/null | jq -r .[]
'')
(writeScriptBin "push-to" ''
if [ -z $2 ]; then
echo "Need 'switch|boot|...'"
exit 1
fi
host=$1
action=$2
${nixos-rebuild} --flake .#$host --build-host root@$host --target-host root@$host $action
'')
(writeScriptBin "option" ''
host=$1
option=$2
nix eval .#nixosConfigurations.$host.config.$option
'')
(writeScriptBin "hm-option" ''
host=$1
option=$2
user=$3
if [[ -z $user ]]; then
user=$USER
fi
nix eval .#nixosConfigurations.$host.config.home-manager.users.$user.$option 2>/dev/null
'')
(writeScriptBin "test-config" ''
#!${stdenv.shell}
for host in $(all-hosts); do
echo == Checking ''${host}..
${nixos-rebuild} --flake .#$host dry-build 2> /tmp/build-output
if [[ $? -ne 0 ]]; then
${bat} --file-name "Failed to verify config for $host" /tmp/build-output
exit 1
fi
done
echo === All checks passed
'')
rebuild
personalCache
allHosts
option
testConfig
inputs.colmena.packages.x86_64-linux.colmena
fup-repl
alejandra

34
scripts/option.sh Normal file
View file

@ -0,0 +1,34 @@
function print_help() {
printf "Usage:"
printf " %s [ --home ] HOST OPTION [ USER ]\n" "$0"
exit 1
}
if [ -z "${2+x}" ]; then
print_help
fi
if [ "$1" = "--home" ]; then
useHome=1
host=$2
if [ -z "${3+x}" ]; then
print_help
fi
option=$3
if [ -n "${4+x}" ]; then
user=$4
else
user=$USER
fi
else
useHome=0
host=$1
option=$2
fi
if [ $useHome -eq 1 ]; then
nix eval ".#nixosConfigurations.$host.config.home-manager.users.$user.$option"
else
nix eval ".#nixosConfigurations.$host.config.$option"
fi

20
scripts/personal-cache.sh Normal file
View file

@ -0,0 +1,20 @@
store="http://elysia-clarki:5000"
action="$1"
function print_help() {
printf "Usage:\n"
printf " %s [ ping | url ]" "$0"
exit 1
}
if [ -z "${action+x}" ]; then
print_help
elif [ "$action" = "--ping" ]; then
nix store ping --store "$store" --option connect-timeout 1 --option download-attempt 1 2>/dev/null
elif [ "$action" = "--url" ]; then
printf "%s" "$store"
else
print_help
fi

48
scripts/rebuild.sh Normal file
View file

@ -0,0 +1,48 @@
function print_help() {
printf "Usage:"
printf " %s [ switch | boot | test | build | ... ] [ HOST ]\n" "$0"
exit 1
}
if [ -z "${1+x}" ]; then
print_help
fi
action=$1
case "$action" in
"switch" | "boot" | "test")
optSudo=sudo
;;
*)
optSudo=
;;
esac
ARGS=( "$action" )
if personal-cache --ping; then
ARGS+=( "--option" "extra-substituters" "$(personal-cache --url)" )
printf "Cache is up and running!\n"
else
printf "Cache is down!\n"
fi
if [ -n "${2+x}" ]; then
host=$2
ARGS+=( "--build-host" "root@$host" "--target-host" "root@$host" "--flake" ".#${host}" )
printf "Building on %s!\n" "$host"
else
ARGS+=( "--flake" "." )
printf "Building on localhost!\n"
fi
today=$(date +%Y-%m-%d)
branch=$(git branch --show-current)
rev=$(git log -1 --format=%h)
dirty=$(git diff --quiet || echo '~')
commit=$(git log --oneline -n1 --format=%B)
export NIXOS_LABEL="$today-$branch-$rev$dirty $commit"
$optSudo nixos-rebuild "${ARGS[@]}"

14
scripts/test-config.sh Normal file
View file

@ -0,0 +1,14 @@
for host in $(all-hosts); do
printf -- "-> Checking %s.. " "$host"
if nixos-rebuild --flake ".#$host" dry-build 2> /tmp/build-output; then
printf "ok\n"
else
printf "failed!\n"
bat --file-name "Failed to verify config for $host" /tmp/build-output
exit 1
fi
done
printf -- "--> All checks passed\n"