summaryrefslogtreecommitdiffstats
path: root/nixos/modules/config/stub-ld.nix
blob: 2e61ad50a18c2e2f79785939315ae61f863df53e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{
  config,
  lib,
  pkgs,
  ...
}:

let
  inherit (lib)
    mkOption
    types
    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
      '';

  stub-ld = stub-ld-for pkgs message;
in
{
  options = {
    environment.stub-ld = {
      enable = mkOption {
        type = types.bool;
        default = true;
        example = false;
        description = ''
          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;
  };

  meta.maintainers = with lib.maintainers; [ tejing ];
}