From 1e978a3edf61b224a958a9ad1d25edf7d3f2c0ca Mon Sep 17 00:00:00 2001 From: technofab Date: Sat, 22 Mar 2025 21:48:54 +0100 Subject: [PATCH] refactor(flake): move setup and finalize script to separate files --- flake.nix | 84 +------------------------------------- scripts/finalize_nix_ci.sh | 37 +++++++++++++++++ scripts/setup_nix_ci.sh | 44 ++++++++++++++++++++ 3 files changed, 83 insertions(+), 82 deletions(-) create mode 100644 scripts/finalize_nix_ci.sh create mode 100644 scripts/setup_nix_ci.sh diff --git a/flake.nix b/flake.nix index b02a85d..8c5560b 100644 --- a/flake.nix +++ b/flake.nix @@ -88,88 +88,8 @@ }; packages = let - setupScript = pkgs.writeShellScriptBin "setup_nix_ci" '' - echo -e "\\e[0Ksection_start:`date +%s`:nix_setup[collapsed=true]\\r\\e[0KSetting up Nix CI" - nix path-info --all > /tmp/nix-store-before - - if [ -z "$NIX_CI_DISABLE_CACHE" ]; then - echo -e "\\e[0Ksection_start:`date +%s`:cache_setup[collapsed=true]\\r\\e[0KConfiguring cache ($NIX_CI_CACHE_STRATEGY)" - case "$NIX_CI_CACHE_STRATEGY" in - "runner") - export RUNNER_CACHE=''${RUNNER_CACHE:-"file://$(pwd)/.nix-cache"} - echo "Runner Cache: $RUNNER_CACHE" - export NIX_CONFIG="$NIX_CONFIG - extra-trusted-substituters = $RUNNER_CACHE?priority=10&trusted=true - extra-substituters = $RUNNER_CACHE?priority=10&trusted=true - " - ;; - "attic") - echo "Attic Cache: $ATTIC_CACHE" - attic login --set-default ci "$ATTIC_SERVER" "$ATTIC_TOKEN" || true - attic use "$ATTIC_CACHE" || true - ;; - "cachix") - echo "Cachix Cache: $CACHIX_CACHE" - cachix use "$CACHIX_CACHE" || true - ;; - "none") - echo "Cache strategy is none, doing nothing..." - ;; - *) - echo "WARNING: Invalid cache strategy set: '$NIX_CI_CACHE_STRATEGY'" - ;; - esac - echo -e "\\e[0Ksection_end:`date +%s`:cache_setup\\r\\e[0K" - else - echo "Caching disabled (NIX_CI_DISABLE_CACHE), skipping cache configuration..." - fi - - # load the job's deps only if the name was passed - if [[ ! -z $1 ]]; then - echo -e "\\e[0Ksection_start:`date +%s`:nix_deps[collapsed=true]\\r\\e[0KFetching Nix dependencies for job" - nix build .#$1 - source $(readlink -f result) - echo -e "\\e[0Ksection_end:`date +%s`:nix_deps\\r\\e[0K" - fi - echo -e "\\e[0Ksection_end:`date +%s`:nix_setup\\r\\e[0K" - ''; - finalizeScript = pkgs.writeShellScriptBin "finalize_nix_ci" '' - echo -e "\\e[0Ksection_start:`date +%s`:finalize_nix_ci[collapsed=true]\\r\\e[0KFinalizing Nix CI..." - nix path-info --all > /tmp/nix-store-after - echo "Finding new paths..." - NEW_PATHS=$(${pkgs.diffutils}/bin/diff --new-line-format="%L" \ - --old-line-format="" --unchanged-line-format="" \ - /tmp/nix-store-before /tmp/nix-store-after) - COUNT=$(${pkgs.busybox}/bin/wc -l <<<"$NEW_PATHS") - if [ -z "$NIX_CI_DISABLE_CACHE" ]; then - echo -e "\\e[0Ksection_start:`date +%s`:cache_push[collapsed=true]\\r\\e[0KPushing $COUNT new store paths to cache ($NIX_CI_CACHE_STRATEGY)" - echo $NEW_PATHS | { - case "$NIX_CI_CACHE_STRATEGY" in - "runner") - export RUNNER_CACHE=''${RUNNER_CACHE:-"file://$(pwd)/.nix-cache"} - # add ^* to all store paths ending in .drv (prevent warning log spam) - ${pkgs.gnused}/bin/sed '/\.drv$/s/$/^*/' | nix copy --quiet --to "$RUNNER_CACHE" --stdin || true - ;; - "attic") - attic push --stdin ci:$ATTIC_CACHE || true - ;; - "cachix") - cachix push $CACHIX_CACHE || true - ;; - "none") - echo "Cache strategy is none, doing nothing..." - ;; - *) - echo "WARNING: Invalid cache strategy set: '$NIX_CI_CACHE_STRATEGY'" - ;; - esac - } - echo -e "\\e[0Ksection_end:`date +%s`:cache_push\\r\\e[0K" - else - echo "Caching disabled, not uploading $COUNT new store entries..." - fi - echo -e "\\e[0Ksection_end:`date +%s`:finalize_nix_ci\\r\\e[0K" - ''; + setupScript = pkgs.writeShellScriptBin "setup_nix_ci" (builtins.readFile ./scripts/setup_nix_ci.sh); + finalizeScript = pkgs.writeShellScriptBin "finalize_nix_ci" (builtins.readFile ./scripts/finalize_nix_ci.sh); in { setup-script = setupScript; finalize-script = finalizeScript; diff --git a/scripts/finalize_nix_ci.sh b/scripts/finalize_nix_ci.sh new file mode 100644 index 0000000..e1200c2 --- /dev/null +++ b/scripts/finalize_nix_ci.sh @@ -0,0 +1,37 @@ +echo -e "\\e[0Ksection_start:`date +%s`:finalize_nix_ci[collapsed=true]\\r\\e[0KFinalizing Nix CI..." + nix path-info --all > /tmp/nix-store-after + echo "Finding new paths..." + NEW_PATHS=$(${pkgs.diffutils}/bin/diff --new-line-format="%L" \ + --old-line-format="" --unchanged-line-format="" \ + /tmp/nix-store-before /tmp/nix-store-after) + COUNT=$(${pkgs.busybox}/bin/wc -l <<<"$NEW_PATHS") + + if [ -z "$NIX_CI_DISABLE_CACHE" ]; then + echo -e "\\e[0Ksection_start:`date +%s`:cache_push[collapsed=true]\\r\\e[0KPushing $COUNT new store paths to cache ($NIX_CI_CACHE_STRATEGY)" + echo $NEW_PATHS | { + case "$NIX_CI_CACHE_STRATEGY" in + "runner") + export RUNNER_CACHE=''${RUNNER_CACHE:-"file://$(pwd)/.nix-cache"} + # add ^* to all store paths ending in .drv (prevent warning log spam) + ${pkgs.gnused}/bin/sed '/\.drv$/s/$/^*/' | nix copy --quiet --to "$RUNNER_CACHE" --stdin || true + ;; + "attic") + attic push --stdin ci:$ATTIC_CACHE || true + ;; + "cachix") + cachix push $CACHIX_CACHE || true + ;; + "none") + echo "Cache strategy is none, doing nothing..." + ;; + *) + echo "WARNING: Invalid cache strategy set: '$NIX_CI_CACHE_STRATEGY'" + ;; + esac + } + echo -e "\\e[0Ksection_end:`date +%s`:cache_push\\r\\e[0K" + else + echo "Caching disabled, not uploading $COUNT new store entries..." + fi +echo -e "\\e[0Ksection_end:`date +%s`:finalize_nix_ci\\r\\e[0K" + diff --git a/scripts/setup_nix_ci.sh b/scripts/setup_nix_ci.sh new file mode 100644 index 0000000..d91e17d --- /dev/null +++ b/scripts/setup_nix_ci.sh @@ -0,0 +1,44 @@ +echo -e "\\e[0Ksection_start:`date +%s`:nix_setup[collapsed=true]\\r\\e[0KSetting up Nix CI" + nix path-info --all > /tmp/nix-store-before + + if [ -z "$NIX_CI_DISABLE_CACHE" ]; then + echo -e "\\e[0Ksection_start:`date +%s`:cache_setup[collapsed=true]\\r\\e[0KConfiguring cache ($NIX_CI_CACHE_STRATEGY)" + case "$NIX_CI_CACHE_STRATEGY" in + "runner") + export RUNNER_CACHE=''${RUNNER_CACHE:-"file://$(pwd)/.nix-cache"} + echo "Runner Cache: $RUNNER_CACHE" + export NIX_CONFIG="$NIX_CONFIG + extra-trusted-substituters = $RUNNER_CACHE?priority=10&trusted=true + extra-substituters = $RUNNER_CACHE?priority=10&trusted=true + " + ;; + "attic") + echo "Attic Cache: $ATTIC_CACHE" + attic login --set-default ci "$ATTIC_SERVER" "$ATTIC_TOKEN" || true + attic use "$ATTIC_CACHE" || true + ;; + "cachix") + echo "Cachix Cache: $CACHIX_CACHE" + cachix use "$CACHIX_CACHE" || true + ;; + "none") + echo "Cache strategy is none, doing nothing..." + ;; + *) + echo "WARNING: Invalid cache strategy set: '$NIX_CI_CACHE_STRATEGY'" + ;; + esac + echo -e "\\e[0Ksection_end:`date +%s`:cache_setup\\r\\e[0K" + else + echo "Caching disabled (NIX_CI_DISABLE_CACHE), skipping cache configuration..." + fi + + # load the job's deps only if the name was passed + if [[ ! -z $1 ]]; then + echo -e "\\e[0Ksection_start:`date +%s`:nix_deps[collapsed=true]\\r\\e[0KFetching Nix dependencies for job" + nix build .#$1 + source $(readlink -f result) + echo -e "\\e[0Ksection_end:`date +%s`:nix_deps\\r\\e[0K" + fi +echo -e "\\e[0Ksection_end:`date +%s`:nix_setup\\r\\e[0K" +