From db6a29862c2f91db7f48a685d91cc7a9ce4cef89 Mon Sep 17 00:00:00 2001 From: Lyes Saadi Date: Sat, 21 Nov 2026 18:39:16 +0100 Subject: [PATCH] Ajouter un script d'auto flake update --- .gitlab-ci.yml | 11 ++++++ flake.nix | 2 +- scripts/update-flake.nix | 85 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 scripts/update-flake.nix diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 21dcae4..75d657d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,3 +9,14 @@ nix-flake-check: stage: test script: - nix flake check --no-build + +# Adapted from https://cobalt.rocks/posts/nix-update/ AGPLv3+ + +nix-flake-update: + stage: build + script: + - nix run .#update-flake + rules: + - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "schedule" + variables: + GIT_STRATEGY: clone diff --git a/flake.nix b/flake.nix index f00085f..af9337a 100644 --- a/flake.nix +++ b/flake.nix @@ -77,7 +77,7 @@ { type = "app"; } - // import (./scripts + "/${name}") { inherit pkgs lib; } + // import (./scripts + "/${name}") { inherit inputs pkgs lib; } ) ) (attrNames (readDir ./scripts)) ); diff --git a/scripts/update-flake.nix b/scripts/update-flake.nix new file mode 100644 index 0000000..7266267 --- /dev/null +++ b/scripts/update-flake.nix @@ -0,0 +1,85 @@ +# Adapted from https://cobalt.rocks/posts/nix-update/ AGPLv3+ + +{ + pkgs, + lib, + inputs, + ... +}: + +{ + meta.description = "Script utilisé pour mettre à jour la flake"; + + program = pkgs.writeShellApplication { + name = "update-flake"; + runtimeInputs = with pkgs; [ + jq + coreutils + gitMinimal + glab + gnugrep + nix + coreutils + # niks3 + ]; + text = '' + # sometimes nix does not play well with ssl certs from the CI runner + # explicitly providing one should make it less flaky + export NIX_SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt + + git switch "$CI_DEFAULT_BRANCH" + + ${lib.toShellVar "inputs" inputs} + ${lib.toShellVar "updated_inputs" [ ]} + + for input in "''${inputs[@]}"; do + if nix flake update "$input" |& tee /dev/stderr | grep -i updated; then + updated_inputs+=("$input") + fi + done + + if [ ''${#updated_inputs[@]} -eq 0 ]; then + echo "No updates for inputs found" + exit 0 + fi + + # if [ -n "''${NIKS3_AUTH_TOKEN_FILE:-}" ]; then + # if ! nix flake archive --json \ + # | jq -r '.inputs | .. | objects | .path? // empty' \ + # | xargs -r niks3 push --auth-token-path "$NIKS3_AUTH_TOKEN_FILE" --max-concurrent-uploads 8; then + # echo "warning: failed to upload the updated flake inputs to the Nix cache" >&2 + # fi + # fi + + if [ "''${CI:-}" = "true" ]; then + git config --global user.name "$GITLAB_USER_LOGIN" + git config --global user.email "$GITLAB_USER_EMAIL" + fi + + # run formatter + nix fmt + + # build commit title/message, this should contain the updated inputs and revs + commit_title="''${updated_inputs[*]}" + commit_title="''${commit_title// /, }" + # commit_title="[ci] Flake update $commit_title" + commit_title="[ci] Flake update $(date '+%d/%m/%Y')" + + commit_message="" + + for input in "''${updated_inputs[@]}"; do + commit_message="$commit_message$input: $(jq -r .nodes.\""$input"\".locked.rev flake.lock)"$'\n' + done + commit_message=''${commit_message%$'\n'} # remove trailing newline from last item + + git commit -a -m "$commit_title" -m "$commit_message" + git push + + # # a commit from a scheduled job (or a CI job in general), will not trigger a pipeline + # # we manually use the gitlab cli to start a pipeline + # # auth snippet from https://gitlab.com/gitlab-org/cli#ci-job-token + # glab auth login --job-token "$CI_JOB_TOKEN" --hostname "$CI_SERVER_HOST" --api-protocol "$CI_SERVER_PROTOCOL" + # GITLAB_HOST="$CI_SERVER_URL" glab ci run-trig -R "$CI_PROJECT_PATH" -b "$CI_DEFAULT_BRANCH" + ''; + }; +}