From a9dbad4d89bc44fbf13fd53bc5586ba5fc2230e3 Mon Sep 17 00:00:00 2001 From: Robert Kovacsics Date: Sat, 13 Jul 2024 19:59:55 +0100 Subject: [PATCH 1/3] nixos/nix-ld: Add nix-ld 32-bit variant --- nixos/modules/programs/nix-ld.nix | 32 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/nixos/modules/programs/nix-ld.nix b/nixos/modules/programs/nix-ld.nix index 770cccd13b504..8eb29d3bc1bbe 100644 --- a/nixos/modules/programs/nix-ld.nix +++ b/nixos/modules/programs/nix-ld.nix @@ -1,22 +1,28 @@ { pkgs, lib, config, ... }: let - cfg = config.programs.nix-ld; + suffixes = [ "nix-ld" "nix-ld-32" ]; + variant = name: { + inherit name; + pkgs = { "nix-ld" = pkgs; "nix-ld-32" = pkgs.pkgsi686Linux; }.${name}; + ldso = { "nix-ld" = "ldso"; "nix-ld-32" = "ldso32"; }.${name}; + cfg = config.programs."${name}"; + }; - nix-ld-libraries = pkgs.buildEnv { + nix-ld-libraries = name: cfg: pkgs: pkgs.buildEnv { name = "ld-library-path"; pathsToLink = [ "/lib" ]; paths = map lib.getLib cfg.libraries; # TODO make glibc here configurable? postBuild = '' - ln -s ${pkgs.stdenv.cc.bintools.dynamicLinker} $out/share/nix-ld/lib/ld.so + ln -s ${pkgs.stdenv.cc.bintools.dynamicLinker} $out/share/${name}/lib/ld.so ''; - extraPrefix = "/share/nix-ld"; + extraPrefix = "/share/${name}"; ignoreCollisions = true; }; in { meta.maintainers = [ lib.maintainers.mic92 ]; - options.programs.nix-ld = { + options.programs = lib.genAttrs suffixes (suffix: let inherit (variant suffix) name cfg pkgs; in { enable = lib.mkEnableOption ''nix-ld, Documentation: ''; package = lib.mkPackageOption pkgs "nix-ld" { }; libraries = lib.mkOption { @@ -25,18 +31,18 @@ in default = [ ]; defaultText = lib.literalExpression "baseLibraries derived from systemd and nix dependencies."; }; - }; + }); - config = lib.mkIf config.programs.nix-ld.enable { - environment.ldso = "${cfg.package}/libexec/nix-ld"; + config = lib.mkMerge (lib.forEach suffixes (suffix: let inherit (variant suffix) name cfg pkgs ldso; in lib.mkIf config.programs.${name}.enable { + environment.${ldso} = "${cfg.package}/libexec/nix-ld"; - environment.systemPackages = [ nix-ld-libraries ]; + environment.systemPackages = [ (nix-ld-libraries name cfg pkgs) ]; - environment.pathsToLink = [ "/share/nix-ld" ]; + environment.pathsToLink = [ "/share/${name}" ]; environment.variables = { - NIX_LD = "/run/current-system/sw/share/nix-ld/lib/ld.so"; - NIX_LD_LIBRARY_PATH = "/run/current-system/sw/share/nix-ld/lib"; + "NIX_LD_${builtins.replaceStrings ["-"] ["_"] pkgs.system}" = "/run/current-system/sw/share/${name}/lib/ld.so"; + "NIX_LD_LIBRARY_PATH_${builtins.replaceStrings ["-"] ["_"] pkgs.system}" = "/run/current-system/sw/share/${name}/lib"; }; # We currently take all libraries from systemd and nix as the default. @@ -57,5 +63,5 @@ in xz systemd ]; - }; + })); } From 095d3e84fc08081d70e77fa25c00f981e8034a99 Mon Sep 17 00:00:00 2001 From: Robert Kovacsics Date: Sat, 13 Jul 2024 20:02:28 +0100 Subject: [PATCH 2/3] nixos/nix-ld: nixpkgs-fmt --- nixos/modules/programs/nix-ld.nix | 76 ++++++++++++++++--------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/nixos/modules/programs/nix-ld.nix b/nixos/modules/programs/nix-ld.nix index 8eb29d3bc1bbe..8bcd142be79f8 100644 --- a/nixos/modules/programs/nix-ld.nix +++ b/nixos/modules/programs/nix-ld.nix @@ -22,46 +22,48 @@ let in { meta.maintainers = [ lib.maintainers.mic92 ]; - options.programs = lib.genAttrs suffixes (suffix: let inherit (variant suffix) name cfg pkgs; in { - enable = lib.mkEnableOption ''nix-ld, Documentation: ''; - package = lib.mkPackageOption pkgs "nix-ld" { }; - libraries = lib.mkOption { - type = lib.types.listOf lib.types.package; - description = "Libraries that automatically become available to all programs. The default set includes common libraries."; - default = [ ]; - defaultText = lib.literalExpression "baseLibraries derived from systemd and nix dependencies."; - }; - }); + options.programs = lib.genAttrs suffixes (suffix: + let inherit (variant suffix) name cfg pkgs; in { + enable = lib.mkEnableOption ''nix-ld, Documentation: ''; + package = lib.mkPackageOption pkgs "nix-ld" { }; + libraries = lib.mkOption { + type = lib.types.listOf lib.types.package; + description = "Libraries that automatically become available to all programs. The default set includes common libraries."; + default = [ ]; + defaultText = lib.literalExpression "baseLibraries derived from systemd and nix dependencies."; + }; + }); - config = lib.mkMerge (lib.forEach suffixes (suffix: let inherit (variant suffix) name cfg pkgs ldso; in lib.mkIf config.programs.${name}.enable { - environment.${ldso} = "${cfg.package}/libexec/nix-ld"; + config = lib.mkMerge (lib.forEach suffixes (suffix: + let inherit (variant suffix) name cfg pkgs ldso; in lib.mkIf config.programs.${name}.enable { + environment.${ldso} = "${cfg.package}/libexec/nix-ld"; - environment.systemPackages = [ (nix-ld-libraries name cfg pkgs) ]; + environment.systemPackages = [ (nix-ld-libraries name cfg pkgs) ]; - environment.pathsToLink = [ "/share/${name}" ]; + environment.pathsToLink = [ "/share/${name}" ]; - environment.variables = { - "NIX_LD_${builtins.replaceStrings ["-"] ["_"] pkgs.system}" = "/run/current-system/sw/share/${name}/lib/ld.so"; - "NIX_LD_LIBRARY_PATH_${builtins.replaceStrings ["-"] ["_"] pkgs.system}" = "/run/current-system/sw/share/${name}/lib"; - }; + environment.variables = { + "NIX_LD_${builtins.replaceStrings ["-"] ["_"] pkgs.system}" = "/run/current-system/sw/share/${name}/lib/ld.so"; + "NIX_LD_LIBRARY_PATH_${builtins.replaceStrings ["-"] ["_"] pkgs.system}" = "/run/current-system/sw/share/${name}/lib"; + }; - # We currently take all libraries from systemd and nix as the default. - # Is there a better list? - programs.nix-ld.libraries = with pkgs; [ - zlib - zstd - stdenv.cc.cc - curl - openssl - attr - libssh - bzip2 - libxml2 - acl - libsodium - util-linux - xz - systemd - ]; - })); + # We currently take all libraries from systemd and nix as the default. + # Is there a better list? + programs.nix-ld.libraries = with pkgs; [ + zlib + zstd + stdenv.cc.cc + curl + openssl + attr + libssh + bzip2 + libxml2 + acl + libsodium + util-linux + xz + systemd + ]; + })); } From abb7313647d5d519fb410011ac14e2a8b2b8b60e Mon Sep 17 00:00:00 2001 From: Robert Kovacsics Date: Tue, 20 Aug 2024 23:09:58 +0100 Subject: [PATCH 3/3] WIP: nix-ld suggestion --- flake.nix | 16 +++ .../manual/release-notes/rl-2205.section.md | 2 +- nixos/modules/programs/nix-ld.nix | 134 ++++++++++++------ 3 files changed, 104 insertions(+), 48 deletions(-) diff --git a/flake.nix b/flake.nix index b22cf56a9b5f7..7d67b608a6798 100644 --- a/flake.nix +++ b/flake.nix @@ -44,6 +44,22 @@ ); }); + systems.x86_64-linux.test = + (self.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + ({ + boot.loader.grub.enable = false; + fileSystems."/".device = "nodev"; + # See https://search.nixos.org/options?show=system.stateVersion&query=stateversion + system.stateVersion = lib.versions.majorMinor lib.version; # DON'T do this in real configs! + + programs.nix-ld.systems.x86_64-linux = { }; + }) + ]; + }); + packages.x86_64-linux.test = self.systems.x86_64-linux.test.config.system.build.toplevel; + checks = forAllSystems (system: { tarball = jobs.${system}.tarball; # Exclude power64 due to "libressl is not available on the requested hostPlatform" with hostPlatform being power64 diff --git a/nixos/doc/manual/release-notes/rl-2205.section.md b/nixos/doc/manual/release-notes/rl-2205.section.md index dad45f12373e6..15e3308362b2c 100644 --- a/nixos/doc/manual/release-notes/rl-2205.section.md +++ b/nixos/doc/manual/release-notes/rl-2205.section.md @@ -125,7 +125,7 @@ In addition to numerous new and upgraded packages, this release has the followin - [nifi](https://nifi.apache.org), an easy to use, powerful, and reliable system to process and distribute data. Available as [services.nifi](#opt-services.nifi.enable). -- [nix-ld](https://github.com/Mic92/nix-ld), Run unpatched dynamic binaries on NixOS. Available as [programs.nix-ld](#opt-programs.nix-ld.enable). +- [nix-ld](https://github.com/Mic92/nix-ld), Run unpatched dynamic binaries on NixOS. Available as `programs.nix-ld`. - [NNCP](http://www.nncpgo.org), NNCP (Node to Node copy) utilities and configuration, Available as [programs.nncp](#opt-programs.nncp.enable). diff --git a/nixos/modules/programs/nix-ld.nix b/nixos/modules/programs/nix-ld.nix index 8bcd142be79f8..661bc20ccb536 100644 --- a/nixos/modules/programs/nix-ld.nix +++ b/nixos/modules/programs/nix-ld.nix @@ -1,69 +1,109 @@ { pkgs, lib, config, ... }: let - suffixes = [ "nix-ld" "nix-ld-32" ]; - variant = name: { - inherit name; - pkgs = { "nix-ld" = pkgs; "nix-ld-32" = pkgs.pkgsi686Linux; }.${name}; - ldso = { "nix-ld" = "ldso"; "nix-ld-32" = "ldso32"; }.${name}; - cfg = config.programs."${name}"; - }; + inherit (lib) + literalExpression + mapAttrsToList + mergeAttrsList + mkEnableOption + mkMerge + mkOption + mkPackageOption + types; + + cfg = config.programs.nix-ld.systems; + + share-path = system: "share/nix-ld-${system}"; - nix-ld-libraries = name: cfg: pkgs: pkgs.buildEnv { + nix-ld-libraries = system: cfg: cfg.pkgs.buildEnv { name = "ld-library-path"; pathsToLink = [ "/lib" ]; paths = map lib.getLib cfg.libraries; # TODO make glibc here configurable? postBuild = '' - ln -s ${pkgs.stdenv.cc.bintools.dynamicLinker} $out/share/${name}/lib/ld.so + ln -s ${cfg.pkgs.stdenv.cc.bintools.dynamicLinker} $out/${share-path system}/lib/ld.so ''; - extraPrefix = "/share/${name}"; + extraPrefix = "/${share-path system}"; ignoreCollisions = true; }; in { meta.maintainers = [ lib.maintainers.mic92 ]; - options.programs = lib.genAttrs suffixes (suffix: - let inherit (variant suffix) name cfg pkgs; in { - enable = lib.mkEnableOption ''nix-ld, Documentation: ''; - package = lib.mkPackageOption pkgs "nix-ld" { }; - libraries = lib.mkOption { - type = lib.types.listOf lib.types.package; - description = "Libraries that automatically become available to all programs. The default set includes common libraries."; - default = [ ]; - defaultText = lib.literalExpression "baseLibraries derived from systemd and nix dependencies."; + + options.programs.nix-ld.systems = mkOption { + default = { }; + description = '' + Configure nix-ld for the given system. + ''; + type = types.attrsOf (types.submodule { + options = { + enable = mkEnableOption "nix-ld for the given system"; + package = mkPackageOption pkgs "nix-ld" { }; + + pkgs = mkOption { + type = types.pkgs; + default = pkgs; + defaultText = "pkgs"; + description = "Package set to use"; + }; + + ldso = mkOption { + type = types.str; + description = '' + Which runtime loader to override, either `ldso` or `ldso32`, see + `environment.ldso` and `environment.ldso32`. + ''; + default = "ldso"; + }; + + libraries = mkOption { + type = types.listOf types.package; + description = "Libraries that automatically become available to all programs. The default set includes common libraries."; + default = [ ]; + defaultText = literalExpression "baseLibraries derived from systemd and nix dependencies."; + }; }; }); + }; - config = lib.mkMerge (lib.forEach suffixes (suffix: - let inherit (variant suffix) name cfg pkgs ldso; in lib.mkIf config.programs.${name}.enable { - environment.${ldso} = "${cfg.package}/libexec/nix-ld"; + config = + let + recursive = + # mkMerge + mergeAttrsList + (mapAttrsToList + (system: cfg: { + environment.${cfg.ldso} = "${cfg.package}/libexec/nix-ld"; - environment.systemPackages = [ (nix-ld-libraries name cfg pkgs) ]; + environment.systemPackages = [ (nix-ld-libraries system cfg) ]; - environment.pathsToLink = [ "/share/${name}" ]; + environment.pathsToLink = [ "/${share-path system}" ]; - environment.variables = { - "NIX_LD_${builtins.replaceStrings ["-"] ["_"] pkgs.system}" = "/run/current-system/sw/share/${name}/lib/ld.so"; - "NIX_LD_LIBRARY_PATH_${builtins.replaceStrings ["-"] ["_"] pkgs.system}" = "/run/current-system/sw/share/${name}/lib"; - }; + environment.variables = { + "NIX_LD_${builtins.replaceStrings ["-"] ["_"] system}" = "/run/current-system/sw/${share-path system}/lib/ld.so"; + "NIX_LD_LIBRARY_PATH_${builtins.replaceStrings ["-"] ["_"] system}" = "/run/current-system/sw/${share-path system}/lib"; + }; - # We currently take all libraries from systemd and nix as the default. - # Is there a better list? - programs.nix-ld.libraries = with pkgs; [ - zlib - zstd - stdenv.cc.cc - curl - openssl - attr - libssh - bzip2 - libxml2 - acl - libsodium - util-linux - xz - systemd - ]; - })); + # We currently take all libraries from systemd and nix as the default. + # Is there a better list? + programs.nix-ld.systems.${system}.libraries = with cfg.pkgs; [ + zlib + zstd + stdenv.cc.cc + curl + openssl + attr + libssh + bzip2 + libxml2 + acl + libsodium + util-linux + xz + systemd + ]; + }) + cfg); + in + # recursive; + { environment.etc.foo.text = builtins.toJSON recursive; }; }