feat: init

This commit is contained in:
Maurice Debray 2025-01-04 16:59:15 +01:00
commit 22d66fe293
No known key found for this signature in database
3 changed files with 102 additions and 0 deletions

14
README.md Normal file
View file

@ -0,0 +1,14 @@
# colmena anywhere
A thin wrapper around [nixos-anywhere](https://github.com/numtide/nixos-anywhere)
to build with colmena
Colmena configurations must use disko.
# Usage
You need to have colmena into your path (and of course all the basic utils you may expect from a linux distro)
```
$ ./colmena-anywhere.sh --help
```

15
colmena-anywhere.nix Normal file
View file

@ -0,0 +1,15 @@
{ pkgs, nodes, node, host, extraArgs, ... }:
let
inherit (nodes.${node}.config.system.build) diskoScript toplevel;
colmenaAnywhere = pkgs.writeShellApplication {
name = "colmena-anywhere-${node}";
runtimeInputs = [ pkgs.nixos-anywhere ];
text = ''
nixos-anywhere -s ${diskoScript} ${toplevel} ${host} ${extraArgs}
'';
};
in {
out = colmenaAnywhere;
drvPath = colmenaAnywhere.drvPath;
runPath = "${colmenaAnywhere}/bin/${colmenaAnywhere.name}";
}

73
colmena-anywhere.sh Executable file
View file

@ -0,0 +1,73 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
usage="$(basename "$0") [-h] [--dryrun] NODE HOST [-- NIXOS_ANYWHERE_EXTRA_ARGS]
Deploy a brand new nixos system using colmena and nixos anywhere
where:
-h Show this help text
--dryrun Print the nixos-anywhere invocation
Exemple:
./colmena-anywhere.sh node root@example.com"
extra_args=""
dry_run=no
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
echo "$usage"
exit 0
;;
--dry-run)
dry_run=y
;;
--)
shift
extra_args="$*"
break
;;
*)
if [[ -z ${node-} ]]; then
node="$1"
elif [[ -z ${host-} ]]; then
host="$1"
else
echo "Wrong arguments. Help:"
echo "$usage"
exit 1
fi
;;
esac
shift
done
if [[ -z ${host-} ]]; then
echo "Wrong arguments. Help:"
echo "$usage"
exit 1
fi
# Get info about the derivation containing the 'nixos-anywhere' invocation
script_infos=$(colmena eval -E "args: import $SCRIPT_DIR/colmena-anywhere.nix (args // { node=\"$node\"; host=\"$host\"; extraArgs=\"$extra_args\"; })")
# realise derivation (because colmena eval only eval stuff)
echo "$script_infos" | jq -r ".drvPath" | xargs nix-store -r
# Run !
command=$(echo "$script_infos" | jq -r ".runPath")
if [ "$dry_run" = "y" ]; then
cat "$command"
else
$command
fi