-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
nixos/stub-ld: init module #269551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
nixos/stub-ld: init module #269551
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| { config, lib, pkgs, ... }: | ||
|
|
||
| let | ||
| inherit (lib) last splitString mkOption types mdDoc optionals; | ||
|
|
||
| libDir = pkgs.stdenv.hostPlatform.libDir; | ||
| ldsoBasename = last (splitString "/" pkgs.stdenv.cc.bintools.dynamicLinker); | ||
|
|
||
| pkgs32 = pkgs.pkgsi686Linux; | ||
| libDir32 = pkgs32.stdenv.hostPlatform.libDir; | ||
| ldsoBasename32 = last (splitString "/" pkgs32.stdenv.cc.bintools.dynamicLinker); | ||
| in { | ||
| options = { | ||
| environment.ldso = mkOption { | ||
| type = types.nullOr types.path; | ||
| default = null; | ||
| description = mdDoc '' | ||
| The executable to link into the normal FHS location of the ELF loader. | ||
| ''; | ||
| }; | ||
|
|
||
| environment.ldso32 = mkOption { | ||
| type = types.nullOr types.path; | ||
| default = null; | ||
| description = mdDoc '' | ||
| The executable to link into the normal FHS location of the 32-bit ELF loader. | ||
|
|
||
| This currently only works on x86_64 architectures. | ||
| ''; | ||
| }; | ||
| }; | ||
|
|
||
| config = { | ||
| assertions = [ | ||
| { assertion = isNull config.environment.ldso32 || pkgs.stdenv.isx86_64; | ||
| message = "Option environment.ldso32 currently only works on x86_64."; | ||
| } | ||
| ]; | ||
|
|
||
| systemd.tmpfiles.rules = ( | ||
| if isNull config.environment.ldso then [ | ||
| "r /${libDir}/${ldsoBasename} - - - - -" | ||
| ] else [ | ||
| "d /${libDir} 0755 root root - -" | ||
| "L+ /${libDir}/${ldsoBasename} - - - - ${config.environment.ldso}" | ||
| ] | ||
| ) ++ optionals pkgs.stdenv.isx86_64 ( | ||
| if isNull config.environment.ldso32 then [ | ||
| "r /${libDir32}/${ldsoBasename32} - - - - -" | ||
| ] else [ | ||
| "d /${libDir32} 0755 root root - -" | ||
| "L+ /${libDir32}/${ldsoBasename32} - - - - ${config.environment.ldso32}" | ||
| ] | ||
| ); | ||
| }; | ||
|
|
||
| meta.maintainers = with lib.maintainers; [ tejing ]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| { config, lib, pkgs, ... }: | ||
|
|
||
| let | ||
| inherit (lib) optionalString mkOption types mdDoc mkIf mkDefault; | ||
|
|
||
| cfg = config.environment.stub-ld; | ||
|
|
||
| message = '' | ||
| NixOS cannot run dynamically linked executables intended for generic | ||
| linux environments out of the box. For more information, see: | ||
| https://nix.dev/permalink/stub-ld | ||
| ''; | ||
|
|
||
| stub-ld-for = pkgsArg: messageArg: pkgsArg.pkgsStatic.runCommandCC "stub-ld" { | ||
| nativeBuildInputs = [ pkgsArg.unixtools.xxd ]; | ||
| inherit messageArg; | ||
| } '' | ||
| printf "%s" "$messageArg" | xxd -i -n message >main.c | ||
| cat <<EOF >>main.c | ||
| #include <stdio.h> | ||
| int main(int argc, char * argv[]) { | ||
| fprintf(stderr, "Could not start dynamically linked executable: %s\n", argv[0]); | ||
| fwrite(message, sizeof(unsigned char), message_len, stderr); | ||
| return 127; // matches behavior of bash and zsh without a loader. fish uses 139 | ||
| } | ||
| EOF | ||
| $CC -Os main.c -o $out | ||
| ''; | ||
|
|
||
| pkgs32 = pkgs.pkgsi686Linux; | ||
|
|
||
| stub-ld = stub-ld-for pkgs message; | ||
| stub-ld32 = stub-ld-for pkgs32 message; | ||
| in { | ||
| options = { | ||
| environment.stub-ld = { | ||
| enable = mkOption { | ||
| type = types.bool; | ||
| default = true; | ||
| example = false; | ||
| description = mdDoc '' | ||
| Install a stub ELF loader to print an informative error message | ||
| in the event that a user attempts to run an ELF binary not | ||
| compiled for NixOS. | ||
| ''; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| config = mkIf cfg.enable { | ||
| environment.ldso = mkDefault stub-ld; | ||
| environment.ldso32 = mkIf pkgs.stdenv.isx86_64 (mkDefault stub-ld32); | ||
| }; | ||
|
|
||
| meta.maintainers = with lib.maintainers; [ tejing ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import ./make-test-python.nix ({ lib, pkgs, ... }: { | ||
| name = "stub-ld"; | ||
|
|
||
| nodes.machine = { lib, ... }: | ||
| { | ||
| environment.stub-ld.enable = true; | ||
|
|
||
| specialisation.nostub = { | ||
| inheritParentConfig = true; | ||
|
|
||
| configuration = { ... }: { | ||
| environment.stub-ld.enable = lib.mkForce false; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| testScript = let | ||
| libDir = pkgs.stdenv.hostPlatform.libDir; | ||
| ldsoBasename = lib.last (lib.splitString "/" pkgs.stdenv.cc.bintools.dynamicLinker); | ||
|
|
||
| check32 = pkgs.stdenv.isx86_64; | ||
| pkgs32 = pkgs.pkgsi686Linux; | ||
|
|
||
| libDir32 = pkgs32.stdenv.hostPlatform.libDir; | ||
| ldsoBasename32 = lib.last (lib.splitString "/" pkgs32.stdenv.cc.bintools.dynamicLinker); | ||
|
|
||
| test-exec = builtins.mapAttrs (n: v: pkgs.runCommand "test-exec-${n}" { src = pkgs.fetchurl v; } "mkdir -p $out;cd $out;tar -xzf $src") { | ||
| x86_64-linux.url = "https://github.com/rustic-rs/rustic/releases/download/v0.6.1/rustic-v0.6.1-x86_64-unknown-linux-gnu.tar.gz"; | ||
| x86_64-linux.hash = "sha256-3zySzx8MKFprMOi++yr2ZGASE0aRfXHQuG3SN+kWUCI="; | ||
| i686-linux.url = "https://github.com/rustic-rs/rustic/releases/download/v0.6.1/rustic-v0.6.1-i686-unknown-linux-gnu.tar.gz"; | ||
| i686-linux.hash = "sha256-fWNiATFeg0B2pfB5zndlnzGn7Ztl8diVS1rFLEDnSLU="; | ||
| aarch64-linux.url = "https://github.com/rustic-rs/rustic/releases/download/v0.6.1/rustic-v0.6.1-aarch64-unknown-linux-gnu.tar.gz"; | ||
| aarch64-linux.hash = "sha256-hnldbd2cctQIAhIKoEZLIWY8H3jiFBClkNy2UlyyvAs="; | ||
| }; | ||
| exec-name = "rustic"; | ||
|
|
||
| if32 = pythonStatement: if check32 then pythonStatement else "pass"; | ||
| in | ||
| '' | ||
| machine.start() | ||
| machine.wait_for_unit("multi-user.target") | ||
|
|
||
| with subtest("Check for stub (enabled, initial)"): | ||
| machine.succeed('test -L /${libDir}/${ldsoBasename}') | ||
| ${if32 "machine.succeed('test -L /${libDir32}/${ldsoBasename32}')"} | ||
|
|
||
| with subtest("Try FHS executable"): | ||
| machine.copy_from_host('${test-exec.${pkgs.system}}','test-exec') | ||
| machine.succeed('if test-exec/${exec-name} 2>outfile; then false; else [ $? -eq 127 ];fi') | ||
| machine.succeed('grep -qi nixos outfile') | ||
| ${if32 "machine.copy_from_host('${test-exec.${pkgs32.system}}','test-exec32')"} | ||
| ${if32 "machine.succeed('if test-exec32/${exec-name} 2>outfile32; then false; else [ $? -eq 127 ];fi')"} | ||
| ${if32 "machine.succeed('grep -qi nixos outfile32')"} | ||
|
|
||
| with subtest("Disable stub"): | ||
| machine.succeed("/run/booted-system/specialisation/nostub/bin/switch-to-configuration test") | ||
|
|
||
| with subtest("Check for stub (disabled)"): | ||
| machine.fail('test -e /${libDir}/${ldsoBasename}') | ||
| ${if32 "machine.fail('test -e /${libDir32}/${ldsoBasename32}')"} | ||
|
|
||
| with subtest("Create file in stub location (to be overwritten)"): | ||
| machine.succeed('mkdir -p /${libDir};touch /${libDir}/${ldsoBasename}') | ||
| ${if32 "machine.succeed('mkdir -p /${libDir32};touch /${libDir32}/${ldsoBasename32}')"} | ||
|
|
||
| with subtest("Re-enable stub"): | ||
| machine.succeed("/run/booted-system/bin/switch-to-configuration test") | ||
|
|
||
| with subtest("Check for stub (enabled, final)"): | ||
| machine.succeed('test -L /${libDir}/${ldsoBasename}') | ||
| ${if32 "machine.succeed('test -L /${libDir32}/${ldsoBasename32}')"} | ||
| ''; | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.