From 0a6f3e992f4e4cde0b3c7f0e92b92c7c4e0bd749 Mon Sep 17 00:00:00 2001 From: winston Date: Tue, 12 Mar 2024 01:39:57 +0100 Subject: [PATCH 1/5] refactor: use `pkgs.writeShellApplication` --- modules/default.nix | 56 +++++++++++---------------------------------- 1 file changed, 13 insertions(+), 43 deletions(-) diff --git a/modules/default.nix b/modules/default.nix index 0f8bf6d..042121e 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -4,7 +4,7 @@ ... }: let inherit (flake-parts-lib) mkPerSystemOption; - inherit (lib) concatStringsSep makeBinPath mapAttrsToList mkOption optional types unique; + inherit (lib) mkOption types; mkNeovimEnv = { config, @@ -12,50 +12,20 @@ ... }: let cfg = config.neovim; - - # TODO: Use wrapProgram? - wrapper = pkgs.writeTextFile rec { - name = "nvim"; - executable = true; - destination = "/bin/${name}"; - text = concatStringsSep "\n" ([ - '' - #!${pkgs.runtimeShell} - set -o errexit - set -o nounset - set -o pipefail - '' - ] - ++ optional (cfg.paths != []) '' - export PATH="$PATH:${makeBinPath (unique cfg.paths)}" - '' - ++ [ - '' - ${concatStringsSep "\n" (mapAttrsToList (name: value: ''export ${name}="''${${name}:-${toString value}}"'') cfg.env)} - '' - ] - ++ [ - '' - export NVIM_RPLUGIN_MANIFEST="${config.neovim.build.rplugin}/rplugin.vim" - ${cfg.package}/bin/nvim -u ${cfg.build.initlua} "$@" - '' - ]); - - checkPhase = '' - runHook preCheck - ${pkgs.stdenv.shellDryRun} "$target" - ${pkgs.shellcheck}/bin/shellcheck "$target" - runHook postCheck - ''; - - meta.mainProgram = name; - }; in - pkgs.buildEnv { - name = "neovim-env"; - paths = [wrapper]; + pkgs.writeShellApplication { + name = "nvim"; + runtimeInputs = cfg.paths; + runtimeEnv = + { + NVIM_RPLUGIN_MANIFEST = "${config.neovim.build.rplugin}/rplugin.vim"; + } + // (cfg.env or {}); meta.mainProgram = "nvim"; - passthru = { + text = '' + ${cfg.package}/bin/nvim -u ${cfg.build.initlua} "$@" + ''; + derivationArgs.passthru = { inherit (config.neovim.build) initlua plugins; }; }; From 70b5d6263b635ca470db73fc9c2e6b8d1f910ca5 Mon Sep 17 00:00:00 2001 From: winston Date: Tue, 12 Mar 2024 03:01:58 +0100 Subject: [PATCH 2/5] refactor: remove `meta.mainProgram` & flip env attrset already set by `writeTextFile`: https://github.com/NixOS/nixpkgs/blob/52544c4a0a84552db4013a8858ade52eaf7ff0ca/pkgs/build-support/trivial-builders/default.nix#L167 --- modules/default.nix | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/default.nix b/modules/default.nix index 042121e..d5024df 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -17,11 +17,10 @@ name = "nvim"; runtimeInputs = cfg.paths; runtimeEnv = - { + (cfg.env or {}) + // { NVIM_RPLUGIN_MANIFEST = "${config.neovim.build.rplugin}/rplugin.vim"; - } - // (cfg.env or {}); - meta.mainProgram = "nvim"; + }; text = '' ${cfg.package}/bin/nvim -u ${cfg.build.initlua} "$@" ''; From f1143483c7759afb74cbc4f7a4f3217ada2171da Mon Sep 17 00:00:00 2001 From: winston Date: Tue, 12 Mar 2024 08:53:44 +0100 Subject: [PATCH 3/5] fix: use previous version of settings `$PATH` --- modules/default.nix | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/default.nix b/modules/default.nix index d5024df..4824319 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -4,7 +4,7 @@ ... }: let inherit (flake-parts-lib) mkPerSystemOption; - inherit (lib) mkOption types; + inherit (lib) makeBinPath mkOption types unique; mkNeovimEnv = { config, @@ -15,15 +15,19 @@ in pkgs.writeShellApplication { name = "nvim"; - runtimeInputs = cfg.paths; runtimeEnv = (cfg.env or {}) // { NVIM_RPLUGIN_MANIFEST = "${config.neovim.build.rplugin}/rplugin.vim"; }; - text = '' - ${cfg.package}/bin/nvim -u ${cfg.build.initlua} "$@" - ''; + text = + lib.optionalString (cfg.paths != []) + '' + export PATH="$PATH:${makeBinPath (unique cfg.paths)}" + '' + + '' + ${cfg.package}/bin/nvim -u ${cfg.build.initlua} "$@" + ''; derivationArgs.passthru = { inherit (config.neovim.build) initlua plugins; }; From 2c4fb982961eb0782fb5a3f73469a450969c9f19 Mon Sep 17 00:00:00 2001 From: Will Ruggiano Date: Tue, 12 Mar 2024 18:59:38 -0600 Subject: [PATCH 4/5] don't use runtimeEnv, update workflows --- .github/workflows/check.yaml | 10 +++------- .github/workflows/update-example.yaml | 3 ++- flake.nix | 15 +++++++++++++++ modules/default.nix | 23 +++++++++++------------ 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 18d5ea8..76761ae 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -1,18 +1,14 @@ -name: Build example and run tests +name: Build and test example flake on: pull_request: - push: - branches: - - main jobs: - tests: + check-pr: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: cachix/install-nix-action@v25 with: github_access_token: ${{ secrets.GITHUB_TOKEN }} - - run: nix build ./example - - run: nix run ./example#test + - run: nix run .#check-local diff --git a/.github/workflows/update-example.yaml b/.github/workflows/update-example.yaml index 93d7e54..7df29fd 100644 --- a/.github/workflows/update-example.yaml +++ b/.github/workflows/update-example.yaml @@ -1,4 +1,4 @@ -name: Update example flake +name: Update, build and test example flake on: push: @@ -17,6 +17,7 @@ jobs: with: github_access_token: ${{ secrets.GITHUB_TOKEN }} - run: nix flake lock --update-input neovim-nix ./example + - run: nix run .#check - run: | git config --global user.name 'github-actions[bot]' git config --global user.email 'willruggiano@users.noreply.github.com' diff --git a/flake.nix b/flake.nix index 0b471ed..0e433b9 100644 --- a/flake.nix +++ b/flake.nix @@ -33,6 +33,21 @@ system, ... }: { + apps = { + check.program = pkgs.writeShellApplication { + name = "check"; + text = '' + nix run ./example#test + ''; + }; + check-local.program = pkgs.writeShellApplication { + name = "check-local"; + text = '' + nix run --override-input neovim-nix path:./. ./example#test + ''; + }; + }; + devShells.default = pkgs.mkShell { name = "neovim.nix"; shellHook = '' diff --git a/modules/default.nix b/modules/default.nix index 4824319..39437ea 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -4,7 +4,7 @@ ... }: let inherit (flake-parts-lib) mkPerSystemOption; - inherit (lib) makeBinPath mkOption types unique; + inherit (lib) concatStringsSep makeBinPath mapAttrsToList mkOption optional optionals types unique; mkNeovimEnv = { config, @@ -15,19 +15,18 @@ in pkgs.writeShellApplication { name = "nvim"; - runtimeEnv = - (cfg.env or {}) - // { - NVIM_RPLUGIN_MANIFEST = "${config.neovim.build.rplugin}/rplugin.vim"; - }; - text = - lib.optionalString (cfg.paths != []) - '' + text = concatStringsSep "\n" ( + optionals (cfg.env != {}) (mapAttrsToList (name: value: ''export ${name}="''${${name}:-${toString value}}"'') cfg.env) + ++ optional (cfg.paths != []) '' export PATH="$PATH:${makeBinPath (unique cfg.paths)}" '' - + '' - ${cfg.package}/bin/nvim -u ${cfg.build.initlua} "$@" - ''; + ++ [ + '' + export NVIM_RPLUGIN_MANIFEST="${config.neovim.build.rplugin}/rplugin.vim" + ${cfg.package}/bin/nvim -u ${cfg.build.initlua} "$@" + '' + ] + ); derivationArgs.passthru = { inherit (config.neovim.build) initlua plugins; }; From 9550fa31a416e4a80b84f79df731af2e76758a9f Mon Sep 17 00:00:00 2001 From: Will Ruggiano Date: Tue, 12 Mar 2024 19:05:06 -0600 Subject: [PATCH 5/5] add some explanatory comments --- modules/default.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/modules/default.nix b/modules/default.nix index 39437ea..648f588 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -12,11 +12,18 @@ ... }: let cfg = config.neovim; + toEnvVar = name: value: ''export ${name}="''${${name}:-${toString value}}"''; in pkgs.writeShellApplication { name = "nvim"; text = concatStringsSep "\n" ( - optionals (cfg.env != {}) (mapAttrsToList (name: value: ''export ${name}="''${${name}:-${toString value}}"'') cfg.env) + # NOTE: We don't use writeShellApplication's `runtimeEnv` argument since + # it does not allow the specified environment variables to be overridden + # (e.g. by direnv). + optionals (cfg.env != {}) (mapAttrsToList toEnvVar cfg.env) + # NOTE: Similar sentiment here. We don't use writeShellApplication's + # `runtimeInputs` because it would *prepend* `cfg.paths`. What we want, + # rather, is to *append* them such that they too can be overridden. ++ optional (cfg.paths != []) '' export PATH="$PATH:${makeBinPath (unique cfg.paths)}" ''