From 4b40063bc2d1690002ee67199659c956145cd7fb Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 1 Jul 2023 00:42:34 +0200 Subject: [PATCH] modules/nixfiles: package script with writeShellApplication --- modules/nixfiles/default.nix | 57 +++----------------- modules/nixfiles/nixfiles-generate-config.sh | 46 ++++++++++++++++ 2 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 modules/nixfiles/nixfiles-generate-config.sh diff --git a/modules/nixfiles/default.nix b/modules/nixfiles/default.nix index 4b66812..07fcfc9 100644 --- a/modules/nixfiles/default.nix +++ b/modules/nixfiles/default.nix @@ -3,55 +3,14 @@ with lib; let - nixfiles-generate-config = pkgs.writeScriptBin "nixfiles-generate-config" '' - #!${pkgs.bash}/bin/bash - set -euo pipefail - - hostname=$(hostname --short) - root="" - ngcroot="" - - while [[ $# -gt 0 ]]; do - case $1 in - --root) - root=$2 - ngcroot="--root ''${root}" - shift - shift - ;; - --hostname) - hostname=$2 - shift - shift - ;; - *) - echo "unknown option: $1" - exit 1 - ;; - esac - done - - mkdir -p ''${root}/etc/nixos - - if [[ ! -d "''${root}/etc/nixos/.git" ]]; then - ${pkgs.git}/bin/git clone https://git.clerie.de/clerie/nixfiles.git ''${root}/etc/nixos - ${pkgs.git}/bin/git -C ''${root}/etc/nixos remote set-url origin gitea@git.clerie.de:clerie/nixfiles.git - fi - - mkdir -p ''${root}/etc/nixos/hosts/''${hostname} - - nixos-generate-config ''${ngcroot} --dir ''${root}/etc/nixos/hosts/''${hostname} - - # make sure host is added to flake.nix - if ! grep -q "''${hostname} = generateNixosSystem \"''${hostname}\";" ''${root}/etc/nixos/flake.nix; then - sed -i "s/\(\s*\)_iso = generateNixosSystem \"_iso\";/\1''${hostname} = generateNixosSystem \"''${hostname}\";\n&/g" ''${root}/etc/nixos/flake.nix - fi - - sed -i "s/\%HOSTNAME\%/''${hostname}/g" ''${root}/etc/nixos/hosts/''${hostname}/configuration.nix - - git -C ''${root}/etc/nixos add ''${root}/etc/nixos/hosts/''${hostname} - ''; - + nixfiles-generate-config = pkgs.writeShellApplication { + name = "nixfiles-generate-config"; + text = builtins.readFile ./nixfiles-generate-config.sh; + runtimeInputs = [ + pkgs.git + ]; + checkPhase = ""; + }; in { options.clerie.nixfiles.enable = mkEnableOption "clerie nixfiles tools"; config = mkIf config.clerie.nixfiles.enable { diff --git a/modules/nixfiles/nixfiles-generate-config.sh b/modules/nixfiles/nixfiles-generate-config.sh new file mode 100644 index 0000000..f26bdb5 --- /dev/null +++ b/modules/nixfiles/nixfiles-generate-config.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -euo pipefail + +hostname=$(hostname --short) +root="" +ngcroot="" + +while [[ $# -gt 0 ]]; do +case $1 in +--root) + root=$2 + ngcroot="--root ${root}" + shift + shift + ;; +--hostname) + hostname=$2 + shift + shift + ;; +*) + echo "unknown option: $1" + exit 1 + ;; +esac +done + +mkdir -p "${root}/etc/nixos" + +if [[ ! -d "${root}/etc/nixos/.git" ]]; then +git clone https://git.clerie.de/clerie/nixfiles.git "${root}/etc/nixos" +git -C "${root}/etc/nixos" remote set-url origin gitea@git.clerie.de:clerie/nixfiles.git +fi + +mkdir -p "${root}/etc/nixos/hosts/${hostname}" + +nixos-generate-config ${ngcroot} --dir "${root}/etc/nixos/hosts/${hostname}" + +# make sure host is added to flake.nix +if ! grep -q "${hostname} = generateNixosSystem \"${hostname}\";" "${root}/etc/nixos/flake.nix"; then +sed -i "s/\(\s*\)_iso = generateNixosSystem \"_iso\";/\1${hostname} = generateNixosSystem \"${hostname}\";\n&/g" "${root}/etc/nixos/flake.nix" +fi + +sed -i "s/\%HOSTNAME\%/${hostname}/g" "${root}/etc/nixos/hosts/${hostname}/configuration.nix" + +git -C "${root}/etc/nixos" add "${root}/etc/nixos/hosts/${hostname}"