1 { config, lib, pkgs, ... }:
4 inherit (lib) optionalString mkOption types mkIf mkDefault;
6 cfg = config.environment.stub-ld;
9 NixOS cannot run dynamically linked executables intended for generic
10 linux environments out of the box. For more information, see:
11 https://nix.dev/permalink/stub-ld
14 stub-ld-for = pkgsArg: messageArg: pkgsArg.pkgsStatic.runCommandCC "stub-ld" {
15 nativeBuildInputs = [ pkgsArg.unixtools.xxd ];
18 printf "%s" "$messageArg" | xxd -i -n message >main.c
21 int main(int argc, char * argv[]) {
22 fprintf(stderr, "Could not start dynamically linked executable: %s\n", argv[0]);
23 fwrite(message, sizeof(unsigned char), message_len, stderr);
24 return 127; // matches behavior of bash and zsh without a loader. fish uses 139
27 $CC -Os main.c -o $out
30 pkgs32 = pkgs.pkgsi686Linux;
32 stub-ld = stub-ld-for pkgs message;
33 stub-ld32 = stub-ld-for pkgs32 message;
36 environment.stub-ld = {
42 Install a stub ELF loader to print an informative error message
43 in the event that a user attempts to run an ELF binary not
50 config = mkIf cfg.enable {
51 environment.ldso = mkDefault stub-ld;
52 environment.ldso32 = mkIf pkgs.stdenv.hostPlatform.isx86_64 (mkDefault stub-ld32);
55 meta.maintainers = with lib.maintainers; [ tejing ];