From d3cb750e6ab75c58a63c4d6c011826ddf8f1bf57 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sat, 3 Aug 2024 18:31:40 +0100 Subject: [PATCH] update-scripts: move out of flake See the explanation in the new update-scripts/README.md file. --- .github/workflows/update.yml | 5 +- flake-modules/default.nix | 3 +- flake-modules/updates/default.nix | 84 ------------------- update-scripts/README.md | 30 +++++++ update-scripts/default.nix | 29 +++++++ .../efmls-configs.nix | 0 update-scripts/generate.nix | 79 +++++++++++++++++ .../updates => update-scripts}/none-ls.nix | 0 .../rust-analyzer.nix | 0 update-scripts/shell.nix | 4 + 10 files changed, 146 insertions(+), 88 deletions(-) delete mode 100644 flake-modules/updates/default.nix create mode 100644 update-scripts/README.md create mode 100644 update-scripts/default.nix rename {flake-modules/updates => update-scripts}/efmls-configs.nix (100%) create mode 100644 update-scripts/generate.nix rename {flake-modules/updates => update-scripts}/none-ls.nix (100%) rename {flake-modules/updates => update-scripts}/rust-analyzer.nix (100%) create mode 100644 update-scripts/shell.nix diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml index f11da711..127581e2 100644 --- a/.github/workflows/update.yml +++ b/.github/workflows/update.yml @@ -64,11 +64,12 @@ jobs: echo "EOF" >> "$GITHUB_OUTPUT" fi - - name: Update autogenerated files + - name: Update generated files id: generate run: | old=$(git show --no-patch --format=%h) - nix run .#generate-files -- --commit + nix-build ./update-scripts -A generate + ./result/bin/generate --commit new=$(git show --no-patch --format=%h) if [ "$old" != "$new" ]; then body=$(git show --no-patch --format=%b) diff --git a/flake-modules/default.nix b/flake-modules/default.nix index dd88406e..45b89487 100644 --- a/flake-modules/default.nix +++ b/flake-modules/default.nix @@ -10,11 +10,10 @@ ./templates.nix ./tests.nix ./wrappers.nix - ./updates ]; perSystem = - { pkgs, system, ... }: + { system, ... }: { _module.args = { pkgsUnfree = import inputs.nixpkgs { diff --git a/flake-modules/updates/default.nix b/flake-modules/updates/default.nix deleted file mode 100644 index 3035e147..00000000 --- a/flake-modules/updates/default.nix +++ /dev/null @@ -1,84 +0,0 @@ -{ - perSystem = - { pkgs, config, ... }: - { - apps.generate-files.program = pkgs.writeShellApplication { - name = "generate-files"; - - runtimeInputs = [ pkgs.nixfmt-rfc-style ]; - - text = '' - repo_root=$(git rev-parse --show-toplevel) - generated_dir=$repo_root/generated - - commit= - while [ $# -gt 0 ]; do - case "$1" in - --commit) commit=1 - ;; - --*) echo "unknown option $1" - ;; - *) echo "unexpected argument $1" - ;; - esac - shift - done - - generate() { - echo "$2" - cp "$1" "$generated_dir/$2.nix" - nixfmt "$generated_dir/$2.nix" - } - - mkdir -p "$generated_dir" - generate "${config.packages.rust-analyzer-options}" "rust-analyzer" - generate "${config.packages.efmls-configs-sources}" "efmls-configs" - generate "${config.packages.none-ls-builtins}" "none-ls" - - if [ -n "$commit" ]; then - cd "$generated_dir" - git add . - - # Construct a msg body from `git status -- .` - body=$( - git status \ - --short \ - --ignored=no \ - --untracked-files=no \ - --no-ahead-behind \ - -- . \ - | sed \ - -e 's/^\s*\([A-Z]\)\s*/\1 /' \ - -e 's/^A/Added/' \ - -e 's/^M/Updated/' \ - -e 's/^R/Renamed/' \ - -e 's/^D/Removed/' \ - -e 's/^/- /' - ) - - # Construct the commit message based on the body - # NOTE: Can't use `wc -l` due to how `echo` pipes its output - count=$(echo -n "$body" | awk 'END {print NR}') - if [ "$count" -gt 1 ] || [ ''${#body} -gt 50 ]; then - msg=$(echo -e "generated: Update\n\n$body") - else - msg="generated:''${body:1}" - fi - - # Commit if there are changes - if [ "$count" -gt 0 ]; then - echo "Committing $count changes..." - echo "$msg" - git commit -m "$msg" --no-verify - fi - fi - ''; - }; - - packages = { - rust-analyzer-options = pkgs.callPackage ./rust-analyzer.nix { }; - efmls-configs-sources = pkgs.callPackage ./efmls-configs.nix { }; - none-ls-builtins = pkgs.callPackage ./none-ls.nix { }; - }; - }; -} diff --git a/update-scripts/README.md b/update-scripts/README.md new file mode 100644 index 00000000..b32e304c --- /dev/null +++ b/update-scripts/README.md @@ -0,0 +1,30 @@ +# Update scripts + +This directory contains update scripts that are not part of the actual flake. + +These scripts are used by the `update` CI workflow, so you shouldn't need to run them manually. +This workflow is found at `.github/workflows/update.yml`. + +## Developing + +Because these scripts aren't packaged in the flake, you should use `nix-build` and `nix-shell` instead of `nix build`, `nix run`, and `nix develop`, etc. + +For example, `nix-build -A generate` will build `./generate.nix` into `./result/bin/generate`. + +A `shell.nix` is available that will place `generate` on your PATH. + +You could use this directory's shell/packages from another working directory by supplying `nix-build` or `nix-shell` with a path. +E.g. `nix-shell ./update-scripts`. + +## Explanation + +These packages are not in the flake outputs for two main reasons: +- Packages built using the flake must follow the flake's `nixConfig` +- Packages included in the flake's output are checked by `nix flake check` + +Being unable to bypass `nixConfig` is an issue because we want to disable [IFD] for the flake, but not for these generate scripts. + +If something changes upstream that causes the builds to fail, we don't want this to block us updating `flake.lock`. +We'd still be made aware of any issues by the `update` CI workflow failing. + +[IFD]: https://nixos.org/manual/nix/stable/language/import-from-derivation diff --git a/update-scripts/default.nix b/update-scripts/default.nix new file mode 100644 index 00000000..25af56b3 --- /dev/null +++ b/update-scripts/default.nix @@ -0,0 +1,29 @@ +{ + # By default, import nixpkgs from flake.lock + pkgs ? + let + lock = (builtins.fromJSON (builtins.readFile ../flake.lock)).nodes.nixpkgs.locked; + nixpkgs = fetchTarball { + url = + assert lock.type == "github"; + "https://github.com/${lock.owner}/${lock.repo}/archive/${lock.rev}.tar.gz"; + sha256 = lock.narHash; + }; + in + import nixpkgs { }, + lib ? pkgs.lib, + ... +}: +lib.fix (self: { + # The main script + default = self.generate; + generate = lib.callPackageWith (pkgs // self) ./generate.nix { }; + + # A shell that has the generate script + shell = pkgs.mkShell { nativeBuildInputs = [ self.generate ]; }; + + # Derivations that build the generated files + efmls-configs-sources = pkgs.callPackage ./efmls-configs.nix { }; + none-ls-builtins = pkgs.callPackage ./none-ls.nix { }; + rust-analyzer-options = pkgs.callPackage ./rust-analyzer.nix { }; +}) diff --git a/flake-modules/updates/efmls-configs.nix b/update-scripts/efmls-configs.nix similarity index 100% rename from flake-modules/updates/efmls-configs.nix rename to update-scripts/efmls-configs.nix diff --git a/update-scripts/generate.nix b/update-scripts/generate.nix new file mode 100644 index 00000000..b46943ca --- /dev/null +++ b/update-scripts/generate.nix @@ -0,0 +1,79 @@ +{ + writeShellApplication, + rust-analyzer-options, + efmls-configs-sources, + none-ls-builtins, + nixfmt-rfc-style, +}: +writeShellApplication { + name = "generate"; + + runtimeInputs = [ nixfmt-rfc-style ]; + + text = '' + repo_root=$(git rev-parse --show-toplevel) + generated_dir=$repo_root/generated + + commit= + while [ $# -gt 0 ]; do + case "$1" in + --commit) commit=1 + ;; + --*) echo "unknown option $1" + ;; + *) echo "unexpected argument $1" + ;; + esac + shift + done + + generate() { + echo "$2" + cp "$1" "$generated_dir/$2.nix" + nixfmt "$generated_dir/$2.nix" + } + + mkdir -p "$generated_dir" + generate "${rust-analyzer-options}" "rust-analyzer" + generate "${efmls-configs-sources}" "efmls-configs" + generate "${none-ls-builtins}" "none-ls" + + if [ -n "$commit" ]; then + cd "$generated_dir" + git add . + + # Construct a msg body from `git status -- .` + body=$( + git status \ + --short \ + --ignored=no \ + --untracked-files=no \ + --no-ahead-behind \ + -- . \ + | sed \ + -e 's/^\s*\([A-Z]\)\s*/\1 /' \ + -e 's/^A/Added/' \ + -e 's/^M/Updated/' \ + -e 's/^R/Renamed/' \ + -e 's/^D/Removed/' \ + -e 's/^/- /' + ) + + # Construct the commit message based on the body + # NOTE: Can't use `wc -l` due to how `echo` pipes its output + count=$(echo -n "$body" | awk 'END {print NR}') + if [ "$count" -gt 1 ] || [ ''${#body} -gt 50 ]; then + msg=$(echo -e "generated: Update\n\n$body") + else + msg="generated:''${body:1}" + fi + + # Commit if there are changes + if [ "$count" -gt 0 ]; then + echo "Committing $count changes..." + echo "$msg" + git commit -m "$msg" --no-verify + fi + fi + ''; +} diff --git a/flake-modules/updates/none-ls.nix b/update-scripts/none-ls.nix similarity index 100% rename from flake-modules/updates/none-ls.nix rename to update-scripts/none-ls.nix diff --git a/flake-modules/updates/rust-analyzer.nix b/update-scripts/rust-analyzer.nix similarity index 100% rename from flake-modules/updates/rust-analyzer.nix rename to update-scripts/rust-analyzer.nix diff --git a/update-scripts/shell.nix b/update-scripts/shell.nix new file mode 100644 index 00000000..0cf5b034 --- /dev/null +++ b/update-scripts/shell.nix @@ -0,0 +1,4 @@ +let + packages = import ./. { }; +in +packages.shell