about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/config/stub-ld.nix
blob: 87b7bdf07a2d04f5a3fc10a307268f340734750e (plain) (blame)
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
{ config, lib, pkgs, ... }:

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

  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 = ''
          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 ];
}