Files
nix/hosts/elara/build-tarball.nix
Nikolaos Karaolidis 8631e15593 Let's hope WSL is not against company policy
If you are looking at this, you know who you are

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
2025-08-19 12:24:17 +03:00

93 lines
2.4 KiB
Nix

{
config,
pkgs,
lib,
...
}:
# FIXME: https://github.com/nix-community/NixOS-WSL/issues/343
# FIXME: https://github.com/nix-community/NixOS-WSL/issues/612
{
system.build.tarballBuilder = lib.mkForce (
pkgs.writeShellApplication {
name = "nixos-wsl-tarball-builder";
runtimeInputs = [
config.nix.package
pkgs.coreutils
pkgs.e2fsprogs
pkgs.gnutar
pkgs.nixos-install-tools
pkgs.pigz
];
text = ''
if ! [ "$EUID" -eq 0 ]; then
echo "This script must be run as root!"
exit 1
fi
out="nixos-wsl.tar.gz"
extra_files=""
positional=()
while [ $# -gt 0 ]; do
case "$1" in
--extra-files=*)
extra_files="''${1#*=}"
;;
--extra-files)
shift
extra_files="$1"
;;
-*)
echo "Unknown option: $1"
echo "Usage: $0 [--extra-files PATH] [output.tar.gz]"
exit 1
;;
*)
positional+=("$1")
;;
esac
shift
done
if [ ''${#positional[@]} -gt 0 ]; then
out="''${positional[0]}"
fi
root=$(mktemp -p "''${TMPDIR:-/tmp}" -d nixos-wsl-tarball.XXXXXXXXXX)
# FIXME: fails in CI for some reason, but we don't really care because it's CI
trap 'chattr -Rf -i "$root" || true && rm -rf "$root" || true' INT TERM EXIT
if [ -n "$extra_files" ]; then
echo "[NixOS-WSL] Copying extra files to $root..."
cp --verbose --archive --no-target-directory "$extra_files" "$root"
fi
chmod o+rx "$root"
echo "[NixOS-WSL] Installing..."
nixos-install \
--root "$root" \
--no-root-passwd \
--system ${config.system.build.toplevel} \
--substituters ""
echo "[NixOS-WSL] Adding channel..."
nixos-enter --root "$root" --command 'HOME=/root nix-channel --add https://github.com/nix-community/NixOS-WSL/archive/refs/heads/main.tar.gz nixos-wsl'
echo "[NixOS-WSL] Compressing..."
tar -C "$root" \
-c \
--sort=name \
--mtime='@1' \
--owner=0 \
--group=0 \
--numeric-owner \
. \
| pigz > "$out"
'';
}
);
}