From 22d66fe293bd0b63ff8328f685531937442731a5 Mon Sep 17 00:00:00 2001 From: sinavir Date: Sat, 4 Jan 2025 16:59:15 +0100 Subject: [PATCH] feat: init --- README.md | 14 +++++++++ colmena-anywhere.nix | 15 +++++++++ colmena-anywhere.sh | 73 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 README.md create mode 100644 colmena-anywhere.nix create mode 100755 colmena-anywhere.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..e12c3d8 --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/colmena-anywhere.nix b/colmena-anywhere.nix new file mode 100644 index 0000000..c127b55 --- /dev/null +++ b/colmena-anywhere.nix @@ -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}"; +} diff --git a/colmena-anywhere.sh b/colmena-anywhere.sh new file mode 100755 index 0000000..03cfcaa --- /dev/null +++ b/colmena-anywhere.sh @@ -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