From b7b913b444795a29a19b2efd7543c10e28fc07c5 Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Thu, 25 Jul 2024 23:25:33 +0800 Subject: [PATCH] feat: add nix-ld and LIBRARY_PATH for mason.nvim and other downloaded binaries --- home/base/tui/editors/neovim/default.nix | 21 +++++++++++++++++++ modules/nixos/desktop/fhs.nix | 26 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/home/base/tui/editors/neovim/default.nix b/home/base/tui/editors/neovim/default.nix index 9d261db0..cb0cea01 100644 --- a/home/base/tui/editors/neovim/default.nix +++ b/home/base/tui/editors/neovim/default.nix @@ -30,6 +30,27 @@ in { viAlias = true; vimAlias = true; + # These environment variables are needed to build and run binaries + # with external package managers like mason.nvim. + # + # LD_LIBRARY_PATH is also needed to run the non-FHS binaries downloaded by mason.nvim. + # it will be set by nix-ld, so we do not need to set it here again. + extraWrapperArgs = with pkgs; [ + # LIBRARY_PATH is used by gcc before compilation to search directories + # containing static and shared libraries that need to be linked to your program. + "--suffix" + "LIBRARY_PATH" + ":" + "${lib.makeLibraryPath [stdenv.cc.cc zlib]}" + + # PKG_CONFIG_PATH is used by pkg-config before compilation to search directories + # containing .pc files that describe the libraries that need to be linked to your program. + "--suffix" + "PKG_CONFIG_PATH" + ":" + "${lib.makeSearchPathOutput "dev" "lib/pkgconfig" [stdenv.cc.cc zlib]}" + ]; + # Currently we use lazy.nvim as neovim's package manager, so comment this one. # # NOTE: These plugins will not be used by astronvim by default! diff --git a/modules/nixos/desktop/fhs.nix b/modules/nixos/desktop/fhs.nix index 456c4d33..019e6909 100644 --- a/modules/nixos/desktop/fhs.nix +++ b/modules/nixos/desktop/fhs.nix @@ -16,4 +16,30 @@ }) ) ]; + + # https://github.com/Mic92/nix-ld + # + # nix-ld will install itself at `/lib64/ld-linux-x86-64.so.2` so that + # it can be used as the dynamic linker for non-NixOS binaries. + # + # nix-ld works like a middleware between the actual link loader located at `/nix/store/.../ld-linux-x86-64.so.2` + # and the non-NixOS binaries. It will: + # + # 1. read the `NIX_LD` environment variable and use it to find the actual link loader. + # 2. read the `NIX_LD_LIBRARY_PATH` environment variable and use it to set the `LD_LIBRARY_PATH` environment variable + # for the actual link loader. + # + # nix-ld's nixos module will set default values for `NIX_LD` and `NIX_LD_LIBRARY_PATH` environment variables, so + # it can work out of the box: + # + # - https://github.com/NixOS/nixpkgs/blob/nixos-24.05/nixos/modules/programs/nix-ld.nix#L37-L40 + # + # You can overwrite `NIX_LD_LIBRARY_PATH` in the environment where you run the non-NixOS binaries to customize the + # search path for shared libraries. + programs.nix-ld = { + enable = true; + libraries = with pkgs; [ + stdenv.cc.cc + ]; + }; }