From fc467b13870389b3182a7b75daa5f9bc3aef6557 Mon Sep 17 00:00:00 2001 From: misumisumi Date: Thu, 27 Jun 2024 13:46:03 +0900 Subject: [PATCH 1/4] feat(nixos): add copyLazyLock option --- nixos/neovim/default.nix | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/nixos/neovim/default.nix b/nixos/neovim/default.nix index 93134d40f..e1a9ebf29 100644 --- a/nixos/neovim/default.nix +++ b/nixos/neovim/default.nix @@ -20,6 +20,11 @@ in Very powerful in terms of keeping the environment consistent, but has the following side effects. You cannot update it even if you run the Lazy command, because it binds read-only. ''; + copyLazyLock = mkEnableOption '' + Copy lazy-lock.json in your repository to $XDG_CONFIG_HOME/nvim. + Achieve environmental consistency while being flexible to change., but has the following side effects. + `lazy-lock.json` is overridden every home-manager activation. + ''; setBuildEnv = mkEnableOption '' Sets environment variables that resolve build dependencies as required by `mason.nvim` and `nvim-treesitter` Environment variables are only visible to `nvim` and have no effect on any parent sessions. @@ -112,6 +117,12 @@ in ]; in mkIf cfg.enable { + assertions = [ + { + assertion = ! (cfg.bindLazyLock && cfg.copyLazyLock); + message = "bindLazyLock and copyLazyLock cannot be enabled at the same time."; + } + ]; xdg.configFile = { "nvim/init.lua".source = ../../init.lua; "nvim/lua".source = ../../lua; @@ -119,6 +130,8 @@ in "nvim/tutor".source = ../../tutor; } // lib.optionalAttrs cfg.bindLazyLock { "nvim/lazy-lock.json".source = ../../lazy-lock.json; + } // lib.optionalAttrs cfg.copyLazyLock { + "nvim/lazy-lock.nix.json".source = ../../lazy-lock.json; }; home = { packages = with pkgs; [ @@ -127,6 +140,10 @@ in shellAliases = optionalAttrs (cfg.setBuildEnv && (lib.versionOlder config.home.stateVersion "24.05")) { nvim = concatStringsSep " " buildEnv + " nvim"; }; + } // lib.optionalAttrs cfg.copyLazyLock { + activation.lazyLockActivatioinAction = lib.hm.dag.entryAfter [ "writeBoundary" ] '' + ${pkgs.jq}/bin/jq -r -s '.[0] * .[1]' ${config.xdg.configHome}/nvim/lazy-lock.json ${config.xdg.configHome}/nvim/lazy-lock.nix.json > ${config.xdg.configHome}/nvim/lazy-lock.json + ''; }; programs.neovim = { enable = true; From d544fa175b15fc94be5cc3c582ae7f7337079dac Mon Sep 17 00:00:00 2001 From: misumisumi Date: Thu, 27 Jun 2024 15:14:24 +0900 Subject: [PATCH 2/4] fix(nixos): fix code style accepted modification proposed by nixd see more nix's code style best practice below url https://nix.dev/guides/best-practices --- nixos/neovim/default.nix | 124 ++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 60 deletions(-) diff --git a/nixos/neovim/default.nix b/nixos/neovim/default.nix index e1a9ebf29..8c9aff17a 100644 --- a/nixos/neovim/default.nix +++ b/nixos/neovim/default.nix @@ -4,8 +4,14 @@ , pkgs , ... }: -with lib; let +let cfg = config.programs.neovim.nvimdots; + inherit (lib) flip warn const; + inherit (lib.attrsets) optionalAttrs; + inherit (lib.options) mkEnableOption mkOption mkIf literalExpression; + inherit (lib.strings) concatStringsSep versionOlder versionAtLeast; + inherit (lib.lists) optionals; + inherit (lib.types) listOf coercedTo package functionTo; in { options = { @@ -39,9 +45,11 @@ in use Haskell plugins. ''; extraHaskellPackages = mkOption { - type = with types; - let fromType = listOf package; - in coercedTo fromType + type = + let + fromType = listOf package; + in + coercedTo fromType (flip warn const '' Assigning a plain list to extraHaskellPackages is deprecated. Please assign a function taking a package set as argument, so @@ -61,7 +69,7 @@ in ''; }; extraDependentPackages = mkOption { - type = with types; listOf package; + type = listOf package; default = [ ]; example = literalExpression "[ pkgs.openssl ]"; description = "Extra build depends to add `LIBRARY_PATH` and `CPATH`."; @@ -72,33 +80,30 @@ in config = let # Inspired from https://github.com/NixOS/nixpkgs/blob/nixos-unstable/nixos/modules/programs/nix-ld.nix - build-dependent-pkgs = with pkgs; builtins.filter (package: !package.meta.unsupported) [ + build-dependent-pkgs = builtins.filter (package: !package.meta.unsupported) [ # manylinux - acl - attr - bzip2 - curl - glibc - libsodium - libssh - libxml2 - openssl - stdenv.cc.cc - stdenv.cc.cc.lib - systemd - util-linux - xz - zlib - zstd + pkgs.acl + pkgs.attr + pkgs.bzip2 + pkgs.curl + pkgs.glibc + pkgs.libsodium + pkgs.libssh + pkgs.libxml2 + pkgs.openssl + pkgs.stdenv.cc.cc + pkgs.stdenv.cc.cc.lib + pkgs.systemd + pkgs.util-linux + pkgs.xz + pkgs.zlib + pkgs.zstd # Packages not included in `nix-ld`'s NixOSModule - glib - libcxx + pkgs.glib + pkgs.libcxx ] ++ cfg.extraDependentPackages; - makePkgConfigPath = x: makeSearchPathOutput "dev" "lib/pkgconfig" x; - makeIncludePath = x: makeSearchPathOutput "dev" "include" x; - neovim-build-deps = pkgs.buildEnv { name = "neovim-build-deps"; paths = build-dependent-pkgs; @@ -128,19 +133,19 @@ in "nvim/lua".source = ../../lua; "nvim/snips".source = ../../snips; "nvim/tutor".source = ../../tutor; - } // lib.optionalAttrs cfg.bindLazyLock { + } // optionalAttrs cfg.bindLazyLock { "nvim/lazy-lock.json".source = ../../lazy-lock.json; - } // lib.optionalAttrs cfg.copyLazyLock { + } // optionalAttrs cfg.copyLazyLock { "nvim/lazy-lock.nix.json".source = ../../lazy-lock.json; }; home = { packages = with pkgs; [ ripgrep ]; - shellAliases = optionalAttrs (cfg.setBuildEnv && (lib.versionOlder config.home.stateVersion "24.05")) { + shellAliases = optionalAttrs (cfg.setBuildEnv && (versionOlder config.home.stateVersion "24.05")) { nvim = concatStringsSep " " buildEnv + " nvim"; }; - } // lib.optionalAttrs cfg.copyLazyLock { + } // optionalAttrs cfg.copyLazyLock { activation.lazyLockActivatioinAction = lib.hm.dag.entryAfter [ "writeBoundary" ] '' ${pkgs.jq}/bin/jq -r -s '.[0] * .[1]' ${config.xdg.configHome}/nvim/lazy-lock.json ${config.xdg.configHome}/nvim/lazy-lock.nix.json > ${config.xdg.configHome}/nvim/lazy-lock.json ''; @@ -151,33 +156,32 @@ in withNodeJs = true; withPython3 = true; - extraPackages = with pkgs; - [ - # Dependent packages used by default plugins - doq - tree-sitter - ] - ++ optionals cfg.withBuildTools [ - cargo - clang - cmake - gcc - gnumake - go - lua51Packages.luarocks - ninja - pkg-config - yarn - ] - ++ optionals cfg.withHaskell [ - (pkgs.writeShellApplication { - name = "stack"; - text = '' - exec "${pkgs.stack}/bin/stack" "--extra-include-dirs=${config.home.profileDirectory}/lib/nvim-depends/include" "--extra-lib-dirs=${config.home.profileDirectory}/lib/nvim-depends/lib" "$@" - ''; - }) - (haskellPackages.ghcWithPackages (ps: cfg.extraHaskellPackages ps)) - ]; + extraPackages = [ + # Dependent packages used by default plugins + pkgs.doq + pkgs.tree-sitter + ] + ++ optionals cfg.withBuildTools [ + pkgs.cargo + pkgs.clang + pkgs.cmake + pkgs.gcc + pkgs.gnumake + pkgs.go + pkgs.lua51Packages.luarocks + pkgs.ninja + pkgs.pkg-config + pkgs.yarn + ] + ++ optionals cfg.withHaskell [ + (pkgs.writeShellApplication { + name = "stack"; + text = '' + exec "${pkgs.stack}/bin/stack" "--extra-include-dirs=${config.home.profileDirectory}/lib/nvim-depends/include" "--extra-lib-dirs=${config.home.profileDirectory}/lib/nvim-depends/lib" "$@" + ''; + }) + (pkgs.haskellPackages.ghcWithPackages (ps: cfg.extraHaskellPackages ps)) + ]; extraPython3Packages = ps: with ps; [ docformatter @@ -185,8 +189,8 @@ in pynvim ]; } - // lib.optionalAttrs (lib.versionAtLeast config.home.stateVersion "24.05") { - extraWrapperArgs = lib.optionals cfg.setBuildEnv [ + // optionalAttrs (versionAtLeast config.home.stateVersion "24.05") { + extraWrapperArgs = optionals cfg.setBuildEnv [ "--suffix" "CPATH" ":" From d4a7086f28ea0c53a3b868679dce58904c2a3b92 Mon Sep 17 00:00:00 2001 From: misumisumi Date: Thu, 27 Jun 2024 16:00:35 +0900 Subject: [PATCH 3/4] fix(nixos): rename copyLazyLock to mergeLazyLock --- nixos/neovim/default.nix | 41 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/nixos/neovim/default.nix b/nixos/neovim/default.nix index 8c9aff17a..341f59cb8 100644 --- a/nixos/neovim/default.nix +++ b/nixos/neovim/default.nix @@ -8,9 +8,10 @@ let cfg = config.programs.neovim.nvimdots; inherit (lib) flip warn const; inherit (lib.attrsets) optionalAttrs; - inherit (lib.options) mkEnableOption mkOption mkIf literalExpression; - inherit (lib.strings) concatStringsSep versionOlder versionAtLeast; inherit (lib.lists) optionals; + inherit (lib.modules) mkIf; + inherit (lib.options) mkEnableOption mkOption literalExpression; + inherit (lib.strings) concatStringsSep versionOlder versionAtLeast; inherit (lib.types) listOf coercedTo package functionTo; in { @@ -25,11 +26,13 @@ in Bind lazy-lock.json in your repository to $XDG_CONFIG_HOME/nvim. Very powerful in terms of keeping the environment consistent, but has the following side effects. You cannot update it even if you run the Lazy command, because it binds read-only. + You need to remove lazy-lock.json before activation, if `mergeLazyLock` have set. ''; - copyLazyLock = mkEnableOption '' - Copy lazy-lock.json in your repository to $XDG_CONFIG_HOME/nvim. - Achieve environmental consistency while being flexible to change., but has the following side effects. - `lazy-lock.json` is overridden every home-manager activation. + mergeLazyLock = mkEnableOption '' + Merge into already existing lazy-lock.json in $XDG_CONFIG_HOME/nvim every activation. + This will respect the package version of lazy-lock.json in the repository. + Achieve environmental consistency while being flexible to change. + You need to unlink lazy-lock.json before activation, if `bindLazyLock` have set. ''; setBuildEnv = mkEnableOption '' Sets environment variables that resolve build dependencies as required by `mason.nvim` and `nvim-treesitter` @@ -124,8 +127,8 @@ in mkIf cfg.enable { assertions = [ { - assertion = ! (cfg.bindLazyLock && cfg.copyLazyLock); - message = "bindLazyLock and copyLazyLock cannot be enabled at the same time."; + assertion = ! (cfg.bindLazyLock && cfg.mergeLazyLock); + message = "bindLazyLock and mergeLazyLock cannot be enabled at the same time."; } ]; xdg.configFile = { @@ -135,20 +138,26 @@ in "nvim/tutor".source = ../../tutor; } // optionalAttrs cfg.bindLazyLock { "nvim/lazy-lock.json".source = ../../lazy-lock.json; - } // optionalAttrs cfg.copyLazyLock { - "nvim/lazy-lock.nix.json".source = ../../lazy-lock.json; + } // optionalAttrs cfg.mergeLazyLock { + "nvim/lazy-lock.fixed.json" = { + source = ../../lazy-lock.json; + onChange = '' + if [ -f ${config.xdg.configHome}/nvim/lazy-lock.json ]; then + tmp=$(mktemp) + ${pkgs.jq}/bin/jq -r -s '.[0] * .[1]' ${config.xdg.configHome}/nvim/lazy-lock.json ${config.xdg.configFile."nvim/lazy-lock.fixed.json".source} > "''${tmp}" && mv "''${tmp}" ${config.xdg.configHome}/nvim/lazy-lock.json + else + ${pkgs.rsync}/bin/rsync --chmod 644 ${config.xdg.configFile."nvim/lazy-lock.fixed.json".source} ${config.xdg.configHome}/nvim/lazy-lock.json + fi + ''; + }; }; home = { - packages = with pkgs; [ - ripgrep + packages = [ + pkgs.ripgrep ]; shellAliases = optionalAttrs (cfg.setBuildEnv && (versionOlder config.home.stateVersion "24.05")) { nvim = concatStringsSep " " buildEnv + " nvim"; }; - } // optionalAttrs cfg.copyLazyLock { - activation.lazyLockActivatioinAction = lib.hm.dag.entryAfter [ "writeBoundary" ] '' - ${pkgs.jq}/bin/jq -r -s '.[0] * .[1]' ${config.xdg.configHome}/nvim/lazy-lock.json ${config.xdg.configHome}/nvim/lazy-lock.nix.json > ${config.xdg.configHome}/nvim/lazy-lock.json - ''; }; programs.neovim = { enable = true; From 16f49f3b7502a2fce5b2a324d5f8b463043ba997 Mon Sep 17 00:00:00 2001 From: misumisumi Date: Sun, 30 Jun 2024 13:06:27 +0900 Subject: [PATCH 4/4] fix(nixos): fix discription --- nixos/neovim/default.nix | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/nixos/neovim/default.nix b/nixos/neovim/default.nix index 341f59cb8..420e7fac3 100644 --- a/nixos/neovim/default.nix +++ b/nixos/neovim/default.nix @@ -26,13 +26,15 @@ in Bind lazy-lock.json in your repository to $XDG_CONFIG_HOME/nvim. Very powerful in terms of keeping the environment consistent, but has the following side effects. You cannot update it even if you run the Lazy command, because it binds read-only. - You need to remove lazy-lock.json before activation, if `mergeLazyLock` have set. + You need to remove lazy-lock.json before enabling this option if `mergeLazyLock` is set. ''; mergeLazyLock = mkEnableOption '' - Merge into already existing lazy-lock.json in $XDG_CONFIG_HOME/nvim every activation. - This will respect the package version of lazy-lock.json in the repository. - Achieve environmental consistency while being flexible to change. - You need to unlink lazy-lock.json before activation, if `bindLazyLock` have set. + Merges the managed lazy-lock.json with the existing one under $XDG_CONFIG_HOME/nvim if its hash has changed on activation. + Upstream package version changes have high priority. + This means changes to lazy-lock.json in the config directory (likely due to installing package) will be preserved. + In other words, it achieves environment consistency while remaining adaptable to changes. + You need to unlink lazy-lock.json before enabling this option if `bindLazyLock` is set. + Please refer to the wiki for details on the behavior. ''; setBuildEnv = mkEnableOption '' Sets environment variables that resolve build dependencies as required by `mason.nvim` and `nvim-treesitter`