1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 inherit (lib)
10 optionalString
11 mkOption
12 types
13 mkIf
14 mkDefault
15 ;
16
17 cfg = config.environment.stub-ld;
18
19 message = ''
20 NixOS cannot run dynamically linked executables intended for generic
21 linux environments out of the box. For more information, see:
22 https://nix.dev/permalink/stub-ld
23 '';
24
25 stub-ld-for =
26 pkgsArg: messageArg:
27 pkgsArg.pkgsStatic.runCommandCC "stub-ld"
28 {
29 nativeBuildInputs = [ pkgsArg.unixtools.xxd ];
30 inherit messageArg;
31 }
32 ''
33 printf "%s" "$messageArg" | xxd -i -n message >main.c
34 cat <<EOF >>main.c
35 #include <stdio.h>
36 int main(int argc, char * argv[]) {
37 fprintf(stderr, "Could not start dynamically linked executable: %s\n", argv[0]);
38 fwrite(message, sizeof(unsigned char), message_len, stderr);
39 return 127; // matches behavior of bash and zsh without a loader. fish uses 139
40 }
41 EOF
42 $CC -Os main.c -o $out
43 '';
44
45 stub-ld = stub-ld-for pkgs message;
46in
47{
48 options = {
49 environment.stub-ld = {
50 enable = mkOption {
51 type = types.bool;
52 default = true;
53 example = false;
54 description = ''
55 Install a stub ELF loader to print an informative error message
56 in the event that a user attempts to run an ELF binary not
57 compiled for NixOS.
58 '';
59 };
60 };
61 };
62
63 config = mkIf cfg.enable {
64 environment.ldso = mkDefault stub-ld;
65 };
66
67 meta.maintainers = with lib.maintainers; [ tejing ];
68}