2019-03-06 13:52:23 +01:00
_DEFAULT_VERSION_TMPL :
echo "{{invocation_directory()}}/nix/variables/versions.tmpl.nix"
2019-03-31 22:47:58 +02:00
_DEFAULT_VERSION :
echo "{{invocation_directory()}}/nix/variables/versions.nix"
2018-11-03 11:45:40 +01:00
_usage :
just -l
2019-11-28 01:05:14 +01:00
# Re-render the default versions
2019-03-31 22:47:58 +02:00
update-default-versions :
#!/usr/bin/env bash
template = " $( just _DEFAULT_VERSION_TMPL) "
outfile = " $( just _DEFAULT_VERSION) "
esh -o ${ outfile } ${ template }
2018-12-17 13:10:06 +01:00
_get_nix_path versionsPath :
2020-10-16 22:30:13 +02:00
echo $( set -x; nix-build --no-link --show-trace { { invocation_directory( ) } } /nix/default.nix -A channelSources --argstr versionsPath { { versionsPath} } )
2018-12-17 13:10:06 +01:00
2018-11-19 02:04:26 +01:00
_device recipe dir +moreargs="" :
2018-10-30 13:38:36 +01:00
#!/usr/bin/env bash
2018-11-03 11:45:40 +01:00
set -ex
2018-12-17 13:10:06 +01:00
source $( just -v _get_nix_path { { invocation_directory( ) } } /{ { dir} } /versions.nix)
2018-12-16 21:57:20 +01:00
$( set -x; nix-build --no-link --show-trace $( dirname { { dir} } ) /default.nix -A recipes.{ { recipe} } --argstr dir { { dir} } { { moreargs} } )
2018-10-30 13:38:36 +01:00
2018-11-10 19:49:25 +01:00
_render_templates :
2018-11-04 22:04:22 +01:00
#!/usr/bin/env bash
2018-11-10 19:49:25 +01:00
set -ex
2020-09-14 19:41:00 +02:00
if ! ip route get 1.1.1.1; then
2019-09-06 12:31:28 +02:00
echo No route to WAN. Skipping template rendering...
else
source $( just -v _get_nix_path { { invocation_directory( ) } } /nix/variables/versions.nix)
nix/scripts/pre-eval-fixed.sh nix/home-manager/profiles/dotfiles/vcsh{ .tmpl,} .nix
fi
2018-11-10 19:49:25 +01:00
_rebuild-device dir rebuildarg="dry-activate" +moreargs="" : _render_templates
#!/usr/bin/env bash
set -ex
2018-11-03 11:45:40 +01:00
just -v _device rebuild { { dir} } --argstr rebuildarg { { rebuildarg} } { { moreargs} }
2018-10-30 13:38:36 +01:00
2019-03-06 13:52:23 +01:00
rebuild-remote-device device target rebuildarg="dry-activate" :
2018-11-12 23:37:31 +01:00
#!/usr/bin/env bash
set -ex
2019-03-06 13:52:23 +01:00
just -v _rebuild-device nix/os/devices/{ { device} } { { rebuildarg} } --argstr moreargs "'--target-host\ {{target}}'"
2018-11-12 23:37:31 +01:00
2019-03-03 00:14:15 +01:00
# Rebuild this device's NixOS
2018-11-03 11:03:03 +01:00
rebuild-this-device rebuildarg="dry-activate" :
2018-11-03 22:55:50 +01:00
#!/usr/bin/env bash
set -e
2018-10-30 13:38:36 +01:00
2018-11-03 22:55:50 +01:00
function parse_hm_rebuildarg( ) {
case $1 in
switch)
echo switch
; ;
*)
echo build
; ;
esac
}
2018-11-03 11:45:40 +01:00
2018-11-03 22:55:50 +01:00
export SYSREBUILD_LOG = .$( hostname -s) _sysrebuild.log
export HOMEREBUILD_LOG = .$( hostname -s) _homerebuild.log
2018-11-03 11:45:40 +01:00
2018-11-03 22:55:50 +01:00
echo Rebuilding system in { { rebuildarg} } -mode...
if just -v _rebuild-device nix/os/devices/$( hostname -s) { { rebuildarg} } > ${ SYSREBUILD_LOG } 2>& 1 ; then
echo System rebuild successful
2018-11-03 11:03:03 +01:00
else
2018-11-03 22:55:50 +01:00
cat ${ SYSREBUILD_LOG }
2019-03-03 00:14:28 +01:00
echo ERROR: system rebuild failed
exit 1
2018-11-03 11:03:03 +01:00
fi
if type home-manager > /dev/null 2>& 1; then
2018-11-03 22:55:50 +01:00
echo Rebuilding home in $( parse_hm_rebuildarg { { rebuildarg} } ) -mode...
2019-01-06 02:05:48 +01:00
source $( just -v _get_nix_path { { invocation_directory( ) } } /nix/os/devices/$( hostname -s) /versions.nix)
2018-11-03 22:55:50 +01:00
if home-manager -v $( parse_hm_rebuildarg { { rebuildarg} } ) > ${ HOMEREBUILD_LOG } 2>& 1 ; then
echo Home rebuild successful
2018-11-03 11:45:40 +01:00
else
2018-11-03 22:55:50 +01:00
cat ${ HOMEREBUILD_LOG }
2019-03-03 00:14:28 +01:00
echo ERROR: home rebuild failed
exit 1
2018-11-03 11:03:03 +01:00
fi
fi
2019-11-28 01:05:14 +01:00
# Re-render the versions of a remote device and rebuild its environment
2021-02-07 17:00:41 +01:00
update-remote-device devicename target rebuildmode='switch' :
2019-03-06 13:52:23 +01:00
#!/usr/bin/env bash
set -e
2021-02-07 17:00:41 +01:00
template = nix/os/devices/{ { devicename } } /versions.tmpl.nix
outfile = nix/os/devices/{ { devicename } } /versions.nix
2019-03-06 13:52:23 +01:00
if ! test -e ${ template } ; then
template = " $( just _DEFAULT_VERSION_TMPL) "
fi
esh -o ${ outfile } ${ template }
if ! test " $( git diff ${ outfile } ) " ; then
echo Already on latest versions
exit 0
fi
2021-02-07 17:00:41 +01:00
just -v rebuild-remote-device { { devicename } } { { target} } dry-activate || {
2019-03-06 13:52:23 +01:00
echo ERROR: rebuild in mode 'dry-active' failed after updating ${ outfile }
exit 1
}
2021-02-07 17:00:41 +01:00
just -v rebuild-remote-device { { devicename } } { { target } } { { rebuildmode } } || {
2019-03-06 13:52:23 +01:00
echo ERROR: rebuild in mode '{{ rebuildmode }}' failed after updating ${ outfile }
exit 1
}
2018-11-03 22:55:50 +01:00
2021-02-07 17:00:41 +01:00
git commit -v ${ outfile } -m "nix/os/devices/{{ devicename }}: bump versions"
2018-11-03 22:55:50 +01:00
2019-11-28 01:05:14 +01:00
# Re-render the versions of the current device and rebuild its environment
2019-01-17 22:35:19 +01:00
update-this-device rebuild-mode='switch' :
2018-11-03 22:55:50 +01:00
#!/usr/bin/env bash
2019-03-03 00:14:28 +01:00
set -e
2019-01-17 22:35:19 +01:00
template = nix/os/devices/$( hostname -s) /versions.tmpl.nix
outfile = nix/os/devices/$( hostname -s) /versions.nix
2019-03-31 22:49:50 +02:00
if ! test -e ${ template } ; then
template = " $( just _DEFAULT_VERSION_TMPL) "
fi
2019-01-17 22:35:19 +01:00
esh -o ${ outfile } ${ template }
if ! test " $( git diff ${ outfile } ) " ; then
echo Already on latest versions
exit 0
fi
2018-11-03 22:55:50 +01:00
export SYSREBUILD_LOG = .$( hostname -s) _sysrebuild.log
2019-01-17 22:35:19 +01:00
just -v rebuild-this-device dry-activate || {
echo ERROR: Update failed, reverting ${ outfile } ...
exit 1
}
2018-11-03 22:55:50 +01:00
2019-01-17 22:35:19 +01:00
just -v rebuild-this-device { { rebuild-mode} } || {
2019-03-03 00:14:28 +01:00
echo ERROR: Rebuilding in { { rebuild-mode} } -mode failed
exit 1
2019-01-17 22:35:19 +01:00
}
2018-11-03 22:55:50 +01:00
2019-03-03 00:14:28 +01:00
git commit -v ${ outfile } -m " nix/os/devices/ $( hostname -s) : bump versions "
2020-09-28 21:56:45 +02:00
# Rebuild an offline system
rebuild-disk device :
#!/usr/bin/env bash
set -xe
just -v disk-mount { { device} }
2020-10-17 15:26:21 +02:00
trap "set +e; just -v disk-umount {{device}}" EXIT
2020-09-28 21:56:45 +02:00
just -v disk-install { { device} }
# Re-render the versions of the given offline system and reinstall it in offline-mode
update-disk dir :
#!/usr/bin/env bash
set -exuo pipefail
dir = { { dir} }
template = { { dir} } /versions.tmpl.nix
outfile = { { dir} } /versions.nix
if ! test -e ${ template } ; then
template = " $( just _DEFAULT_VERSION_TMPL) "
fi
esh -o ${ outfile } ${ template }
if ! test " $( git diff ${ outfile } ) " ; then
echo Already on latest versions
exit 0
fi
export SYSREBUILD_LOG = .{ { dir} } _sysrebuild.log
just -v rebuild-disk { { dir} } || {
echo ERROR: Update of { { dir} } failed, reverting ${ outfile } ...
exit 1
}
git commit -v ${ outfile } -m " ${ dir } : bump versions "
2018-11-15 18:24:28 +01:00
# Iterate on a qtile config by running it inside Xephyr. (un-/grab the mouse with Ctrl + Shift-L)
2018-11-03 18:43:22 +01:00
hm-iterate-qtile :
#!/usr/bin/env bash
set -xe
2018-11-04 14:07:02 +01:00
home-manager switch || just -v rebuild-this-device switch
2018-11-03 18:43:22 +01:00
Xephyr -ac -br -resizeable :1 &
XEPHYR_PID = $!
echo ${ XEPHYR_PID }
DISPLAY = :1 $( grep qtile ~/.xsession) &
2019-06-30 20:16:17 +02:00
echo "Xephyr started. un-/grab the mouse with Ctrl + Shift-L"
2018-11-03 18:43:22 +01:00
wait $!
kill ${ XEPHYR_PID }
2018-11-10 19:24:24 +01:00
# !!! DANGERIOUS !!! This wipes the disk which is configured for the given device.
disk-prepare dir :
2020-12-31 02:12:29 +01:00
just -v _device diskPrepare { { dir} }
2018-11-03 11:03:03 +01:00
2019-01-12 22:24:30 +01:00
disk-relabel dir previous :
2020-12-31 02:12:29 +01:00
just -v _device diskRelabel { { dir} } --argstr previousDiskId { { previous} }
2019-01-12 22:24:30 +01:00
2018-11-03 11:03:03 +01:00
# Mount the target disk specified by device configuration directory. The 'dir' argument points to a device configuration, e.g. 'nix/os/devices/steveej-live-mmc-SL32G_0x259093f6'
disk-mount dir :
2020-12-31 02:12:29 +01:00
just -v _device diskMount { { dir} }
2018-11-03 11:03:03 +01:00
# Unmount target disk, specified by device configuration directory
disk-umount dir :
2020-12-31 02:12:29 +01:00
just -v _device diskUmount { { dir} }
2018-11-03 11:03:03 +01:00
2018-11-10 19:24:24 +01:00
# Perform an offline installation on the mounted target disk, specified by device configuration directory
2018-11-10 19:49:25 +01:00
disk-install dir : _render_templates
2020-12-31 02:12:29 +01:00
just -v _device diskInstall { { dir} }
2018-11-19 18:25:06 +01:00
2021-12-06 21:38:27 +01:00
2019-01-10 23:31:33 +01:00
verify-n-unlock sshserver attempts="10" :
2021-12-06 21:38:27 +01:00
#!/usr/bin/env bash
set -e
env \
GETPW = "just _get_pass_entry Infrastructure/VPS/{{sshserver}} DRIVE_PW" \
SSHOPTS = " $( just _get_pass_entry Infrastructure/VPS/{ { sshserver} } SSHOPTS) " \
VNCSOCK = " $( just _get_pass_entry Infrastructure/VPS/{ { sshserver} } VNCSOCK) " \
VNCPW = " $( just _get_pass_entry Infrastructure/VPS/{ { sshserver} } VNCPW) " \
\
just _verify-n-unlock { { sshserver} } { { attempts} }
_verify-n-unlock sshserver attempts :
2018-11-19 18:25:06 +01:00
#!/usr/bin/env bash
set -e
2018-12-17 13:38:12 +01:00
: ${ VNCSOCK : ?VNCSOCK must be set }
: ${ VNCPW : ?VNCPW must be set }
2018-11-19 18:25:06 +01:00
export MAGICK_ARGS = "-filter Catrom -density 72 -resample 300 -contrast -normalize -despeckle -type grayscale -sharpen 1 -posterize 3 -negate -gamma 100 -blur 1x65535"
export TESS_ARGS = "-c debug_file=/dev/null --psm 4"
2018-12-17 13:38:12 +01:00
function send( ) {
local what = " ${ 1 : ?need something to send } "
ssh -4 ${ SSHOPTS : ?need sshopts } root@{ { sshserver} } " echo -e ${ what } >> /dev/tty0 " & >/dev/null
}
function expect( ) {
local what = " ${ 1 : ?need something to expect } "
2018-11-19 18:25:06 +01:00
vncdo --server= ${ VNCSOCK } --password= ${ VNCPW } --disable-desktop-resizing --nocursor capture $PWD /screenshot.bmp
convert ${ MAGICK_ARGS } screenshot.bmp screenshot.tiff
tesseract ${ TESS_ARGS } screenshot.tiff screenshot
2018-12-17 13:38:12 +01:00
grep --quiet " ${ what } " screenshot.txt
}
function send_and_expect( ) {
local send = " ${ 1 : ?need something to send } "
local expect = " ${ 2 : ?need something to expect } "
2019-01-02 12:19:55 +01:00
if ! send " ${ send } " ; then
echo warning: cannot send > /dev/stderr
return -1
fi
2018-12-17 13:38:12 +01:00
expect " ${ expect } "
2018-11-19 18:25:06 +01:00
}
2018-12-17 13:38:12 +01:00
trap 'E=$?; set +e; rm screenshot.*; echo Exiting...; kill $(jobs -p | cut -d " " -f 4); exit $E' EXIT
2018-11-19 18:25:06 +01:00
for i in ` seq 1 { { attempts} } ` ; do
2018-12-17 13:38:12 +01:00
echo Attempt $i ...
expect = " $( pwgen -0 12) "
send = " '\0033\0143' ${ expect } "
if send_and_expect " ${ send } " " ${ expect } " ; then
pipe = $( mktemp -u)
mkfifo ${ pipe }
exec 3<>${ pipe }
rm ${ pipe }
2018-11-19 18:25:06 +01:00
echo Verification succeeded at attempt $i . Unlocking remote drive...
2018-12-17 13:38:12 +01:00
ssh -4 ${ SSHOPTS } root@{ { sshserver} } "cryptsetup-askpass" <& 3 & >/dev/null &
eval ${ GETPW } | head -n1 >& 3
for j in ` seq 1 120` ; do
sleep 0.5
if expect '— success' ; then
echo Unlock successful.
exit 0
fi
done
2018-12-12 11:02:46 +01:00
echo Unlock failed...
2018-12-17 13:38:12 +01:00
exit 1
2018-11-19 18:25:06 +01:00
fi
done
echo Verification failed { { attempts} } times. Giving up...
exit 1
2018-12-17 13:38:12 +01:00
_get_pass_entry path key :
2021-08-21 02:44:05 +02:00
pass show { { path} } | grep -E "^{{key}}:" | sed -E 's/^[^:]+: *//g'
2019-01-17 22:43:11 +01:00
2019-01-12 22:34:08 +01:00
run-with-channels +cmds :
2019-01-17 22:43:11 +01:00
#!/usr/bin/env bash
2019-01-12 22:34:08 +01:00
source $( just -v _get_nix_path { { invocation_directory( ) } } /nix/variables/versions.nix)
{ { cmds} }
2019-07-17 09:46:38 +02:00
2020-11-25 21:51:27 +01:00
install-config config root :
sudo just run-with-channels nixos-install -I nixos-config= { { invocation_directory( ) } } /{ { config} } --root { { root} } --no-root-passwd
2019-09-06 12:39:24 +02:00
# Switch between gpg-card capable devices which have a copy of the same key
switch-gpg-card :
2019-07-17 09:46:38 +02:00
#!/usr/bin/env bash
2019-09-06 12:31:10 +02:00
#
# Derived from https://github.com/drduh/YubiKey-Guide/issues/19.
#
# Connect the new device and then run this script to make it known to gnupg.
#
2019-07-17 09:46:38 +02:00
set -xe
KEY_ID = $( gpg --card-status | rg sec | rg -o '[0-9A-Z]{16}' )
2019-09-06 12:31:10 +02:00
# export pubkey and ownertrust
gpg2 --output " ${ KEY_ID } " .pubkey --export " ${ KEY_ID } "
2020-11-24 22:59:33 +01:00
# if this fails the trust in the key is missing and can be fixed with `gpg --edit-key ${KEY_ID}`
2019-09-06 12:31:10 +02:00
gpg2 --export-ownertrust | rg " ${ KEY_ID } " > " ${ KEY_ID } " .ownertrust
# delete the key
gpg --yes --delete-secret-and-public-keys " ${ KEY_ID } "
# import pubkey and ownertrust back and cleanup
gpg2 --import " ${ KEY_ID } " .pubkey
gpg2 --import-ownertrust < " ${ KEY_ID } " .ownertrust
rm " ${ KEY_ID } " .{ pubkey,ownertrust}
# refresh the gpg agent
2019-07-17 09:46:38 +02:00
gpg-connect-agent "scd serialno" "learn --force" /bye
gpg --card-status
2020-12-16 10:53:51 +01:00
# Connect to `remote` UUID, and turn it into a short name
uuid-to-device-name remote :
#!/usr/bin/env bash
set -e -o pipefail
ssh { { remote} } 'nix run nixpkgs.dmidecode -c dmidecode -s system-uuid' | xxhsum --quiet -H1 | awk '{print $1}'