Help with debugging: 'nix build' and 'nixos-rebuild' fail without error message

I have a working NixOS config (25.11) and I tried to add my own package to my flake.nix. It’s not my first package but I’m still new to NixOS, so I came up with this (with some AI assistance):

{
  lib,
  stdenvNoCC,
  fetchurl,
  makeWrapper,
  jre,
}:

let
  pname = "megameklab";
  version = "0.50.11";

  src = fetchurl {
    url = "https://github.com/MegaMek/megameklab/releases/download/v${version}/megameklab-${version}.tar.gz";
    hash = "sha256-1w70phkwk44hcs1lw20kfhq17lnshjrg8j644v7vdldyfn5djx41";
  };
in
stdenvNoCC.mkDerivation {
  inherit pname version src;

  nativeBuildInputs = [ makeWrapper ];

  dontBuild = true;

  installPhase = ''
    runHook preInstall

    mkdir -p $out/share/${pname}
    cp -R ./* $out/share/${pname}/

    # ensure upstream launch script is executable
    chmod +x $out/share/${pname}/bin/MegaMekLab

    mkdir -p $out/bin

    upstream_launcher="$out/share/${pname}/bin/MegaMekLab"
    if [ ! -f "$upstream_launcher" ]; then
      echo "Could not find upstream Gradle launcher script at: $upstream_launcher"
      exit 1
    fi

    # Wrap the upstream script:
    # - --set JAVA_HOME: makes the Gradle script find the correct java
    # - --set JAVACMD: forces the exact java binary (robust even if JAVA_HOME logic changes)
    # - --chdir: matches typical upstream invocation from the app root dir
    makeWrapper "$upstream_launcher" "$out/bin/megameklab" \
      --set JAVA_HOME "${jre}" \
      --set JAVACMD "${jre}/bin/java" \
      --chdir "$out/share/${pname}"

    runHook postInstall
  '';

  meta = with lib; {
    description = "MegaMekLab";
    homepage = "https://github.com/MegaMek/megameklab";
    license = licenses.gpl3;
    platforms = platforms.all;
    mainProgram = "megameklab";
  };
}

However, when I try to build the package, the command just silently fails. It produces no output at all (except for the usual “dirty repo” warning):

nix --extra-experimental-features "nix-command flakes" build .#megameklab -L -v --show-trace --print-build-logs --print-out-paths --rebuild
warning: Git tree '/etc/nixos' is dirty
root nixos on  master [+]

If I import the package into a module and run nixos-rebuild switch I get this error:

Command 'nix --extra-experimental-features 'nix-command flakes' build --print-out-paths '/etc/nixos#nixosConfigurations."nixvm".config.system.build.toplevel' --no-link' returned non-zero exit status 1.

I’ve already checked if the OOM killer has been involved (I had that issue in the past) but it isn’t the case. I’ve also tried to build the package on a real machine (instead of VM), but it failed there too.

I’ve tried all debug / verbose flags I could find, but after hundreds of lines of evaluating XXX the output abruptly ends without an error message (and exit code 1).

After I ran out of ideas, I tried to debug this with the help of GPT5.2 and aider. I was surprised how structured the AI approached the debugging (I had 0 expectations from past experiences) but after 2 hours I gave up. The only thing I found out is that nix exits very early in the build process, without printing any error message, but I still have no idea why.

How would you debug this?

My first step would be to add a set -x or echo I was here; exit 42 to the installPhase. That way you can check if the install phase script is at all executed.

A minimal check could also be that you “build” some default derivation from an upstream flake:

nix --extra-experimental-features "nix-command flakes" build nixpkgs#hello
ls -l result*
rm result*
nix --extra-experimental-features "nix-command flakes" build nixpkgs#hello --rebuild
ls -l result*

You could also try pkgs.runCommand instead of stdenvNoCC.mkDerivation for very simple packages.

And you can check that the evaluation reaches your code by putting a trace in there:

# ...
let # ...
in 
builtins.trace "I was here"
stdenvNoCC.mkDerivation {
# ...

Thank you! I’ve tried what you suggested. Building (and re-building) hello works. The “I was here” trace (right before the derivation) is also printed. However, no matter what I add to the installPhase is not printed (set -x, echo “FOO”, etc). Looks like that script is never executed.

I’ve tried to replace stdenvNoCC.mkDerivationwith pkgs.runCommand, but that results in errors. I’ve never used that command before, but I guess it does not return a derivation and thus is not suitable as drop-in replacement?

No it is not a drop in replacement, see the docs: Nixpkgs Reference Manual

But it produces a derivation so you should be able to use it.

Sth. like this?

{                                                                                                                                                                                                                                                                                                                    
  1   lib,                                                                                                                                                                                                                                                                                                               
  2   pkgs,                                                                                                                                                                                                                                                                                                              
  3   fetchurl,                                                                                                                                                                                                                                                                                                          
  4   makeWrapper,                                                                                                                                                                                                                                                                                                       
  5   jre,                                                                                                                                                                                                                                                                                                               
  6 }:                                                                                                                                                                                                                                                                                                                   
  7                                                                                                                                                                                                                                                                                                                      
  8 let                                                                                                                                                                                                                                                                                                                  
  9   pname = "megameklab";                                                                                                                                                                                                                                                                                              
 10   version = "0.50.11";                                                                                                                                                                                                                                                                                               
 11                                                                                                                                                                                                                                                                                                                      
 12   src = fetchurl {                                                                                                                                                                                                                                                                                                   
 13     url = "https://github.com/MegaMek/megameklab/releases/download/v${version}/megameklab-${version}.tar.gz";                                                                                                                                                                                                        
 14     hash = "sha256-1w70phkwk44hcs1lw20kfhq17lnshjrg8j644v7vdldyfn5djx41";                                                                                                                                                                                                                                            
 15   };                                                                                                                                                                                                                                                                                                                 
 16 in                                                                                                                                                                                                                                                                                                                   
 17 (pkgs.runCommand pname                                                                                                                                                                                                                                                                                               
 18   {                                                                                                                                                                                                                                                                                                                  
 19     inherit src;                                                                                                                                                                                                                                                                                                     
 20                                                                                                                                                                                                                                                                                                                      
 21     buildInputs = [ makeWrapper ];                                                                                                                                                                                                                                                                                   
 22                                                                                                                                                                                                                                                                                                                      
 23     inherit pname version;                                                                                                                                                                                                                                                                                           
 24   }                                                                                                                                                                                                                                                                                                                  
 25   ''                                                                                                                                                                                                                                                                                                                 
 26     set -euo pipefail                                                                                                                                                                                                                                                                                                
 27                                                                                                                                                                                                                                                                                                                      
 28     set -x; echo "FOO"; exit 42                                                                                                                                                                                                                                                                                      
 29                                                                                                                                                                                                                                                                                                                      
 30     mkdir -p "$out/share/${pname}"                                                                                                                                                                                                                                                                                   
 31     cp -R ./* "$out/share/${pname}/"                                                                                                                                                                                                                                                                                 
 32                                                                                                                                                                                                                                                                                                                      
 33     chmod +x "$out/share/${pname}/bin/MegaMekLab"                                                                                                                                                                                                                                                                    
 34                                                                                                                                                                                                                                                                                                                      
 35     mkdir -p "$out/bin"                                                                                                                                                                                                                                                                                              
 36                                                                                                                                                                                                                                                                                                                      
 37     upstream_launcher="$out/share/${pname}/bin/MegaMekLab"                                                                                                                                                                                                                                                           
 38     if [ ! -f "$upstream_launcher" ]; then                                                                                                                                                                                                                                                                           
 39       echo "Could not find upstream Gradle launcher script at: $upstream_launcher" >&2                                                                                                                                                                                                                               
 40       exit 1                                                                                                                                                                                                                                                                                                         
 41     fi                                                                                                                                                                                                                                                                                                               
 42                                                                                                                                                                                                                                                                                                                      
 43     makeWrapper "$upstream_launcher" "$out/bin/megameklab" \                                                                                                                                                                                                                                                         
 44       --set JAVA_HOME "${jre}" \                                                                                                                                                                                                                                                                                     
 45       --set JAVACMD "${jre}/bin/java" \                                                                                                                                                                                                                                                                              
 46       --chdir "$out/share/${pname}"                                                                                                                                                                                                                                                                                  
 47   ''                                                                                                                                                                                                                                                                                                                 
 48 ).overrideAttrs                                                                                                                                                                                                                                                                                                      
 49   (_: {                                                                                                                                                                                                                                                                                                              
 50     passthru.meta = with lib; {                                                                                                                                                                                                                                                                                      
 51       description = "MegaMekLab";                                                                                                                                                                                                                                                                                    
 52       homepage = "https://github.com/MegaMek/megameklab";                                                                                                                                                                                                                                                            
 53       license = licenses.gpl3;                                                                                                                                                                                                                                                                                       
 54       platforms = platforms.all;                                                                                                                                                                                                                                                                                     
 55       mainProgram = "megameklab";                                                                                                                                                                                                                                                                                    
 56     };                                                                                                                                                                                                                                                                                                               
 57   })

Doesn’t make any difference, though. Still exits silently with code 1.

I’ve also tried to rebuild 2 of the other packages I’ve created (very simple ones, just unpacking an AppImage) and they work (same flake.nix).

So, I guess the question is: how does this package manage to crash nix build after starting evaluation but before running the install script? And why is there no error message?

The runCommand usage looks ok.

Next I would look at the way you call this file. I guess via pkgs.callPackage given the signature (in which case you could also replace pkgs in the function argument and the call to pkgs.runCommand which just runCommand in both cases). Maybe there is some bug there. Can you post the calling code?

It should also be possible to put the meta stuff into the second argument to runCommand instead of the overrideAttrs call.

Here’s my flake.nix:

{
  description = "Configuration Flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
    home-manager = {
      url = "github:nix-community/home-manager/release-25.11";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    disko = {
      url = "github:nix-community/disko";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    agenix = {
      url = "github:ryantm/agenix";
      inputs.nixpkgs.follows = "nixpkgs";
      inputs.darwin.follows = "";
    };
    rycee-firefox-addons = {
      url = "gitlab:rycee/nur-expressions?dir=pkgs/firefox-addons";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    nix-flatpak = {
      url = "github:gmodena/nix-flatpak/?ref=latest";
    };
    nix-ai = {
      url = "github:olafkfreund/nix-ai-help";
    };
  };

  outputs =
    inputs@{
      nixpkgs,
      home-manager,
      disko,
      agenix,
      rycee-firefox-addons,
      nix-flatpak,
      nix-ai,
      ...
    }:
    let
      system = "x86_64-linux";
      firefox-addons = rycee-firefox-addons.packages.${system};
      buildXpiAddon = rycee-firefox-addons.lib.${system}.buildFirefoxXpiAddon;
      specialArgs = { inherit inputs system; };

      overlays = [
        (import ./pkgs/stl-thumb/overlay.nix)
        (import ./pkgs/orynt3d/overlay.nix)
        (import ./pkgs/exifsort/overlay.nix)
        (import ./pkgs/megameklab/overlay.nix)
      ];

      pkgs = import nixpkgs {
        inherit system overlays;
      };

      def_modules = [
        home-manager.nixosModules.default
        {
          home-manager = {
            extraSpecialArgs = {
              inherit
                firefox-addons
                buildXpiAddon
                agenix
                nix-ai
                ;
            };
            useGlobalPkgs = true;
            useUserPackages = true;
            backupFileExtension = "hm_backup";
          };
        }
        agenix.nixosModules.default
        {
          age = {
            identityPaths = [ "/etc/ssh/id_agenix" ];
            secrets.smbcredentials = {
              file = ./secrets/smbcredentials.age;
              owner = "root";
              mode = "400";
            };
            secrets.ai_api_keys = {
              file = ./secrets/ai_api_keys.age;
              name = "ai_api_keys.env";
              owner = "root";
              group = "ai";
              mode = "440";
            };
          };
        }
        nix-flatpak.nixosModules.nix-flatpak
        {
          nixpkgs.overlays = overlays;
        }
      ];
    in
    {
      packages.${system} = with pkgs; {
        inherit stl-thumb orynt3d exifsort megameklab;
      };

      nixosConfigurations = {
        "lappi" = nixpkgs.lib.nixosSystem {
          inherit system specialArgs;
          modules = def_modules ++ [
            ./hosts/lappi/configuration.nix
          ];
        };
        "nixvm" = nixpkgs.lib.nixosSystem {
          inherit system specialArgs;
          modules = def_modules ++ [
            ./hosts/nixvm/configuration.nix
            disko.nixosModules.disko
          ];
        };
      };
    };
}

The overlay.nix is basically identical for all packages:

final: prev: {
  megameklab = final.callPackage ./default.nix { };
}

As you can see, all my packages are built the same way, but only this one fails (it’s also the only one where I use mkDerivation directly). Since the install script is never executed, is it safe to assume that the problem hides somewhere within the following lines?

{
  lib,
  stdenvNoCC,
  fetchurl,
  makeWrapper,
  jre,
}:

let
  pname = "megameklab";
  version = "0.50.11";

  src = fetchurl {
    url = "https://github.com/MegaMek/megameklab/releases/download/v${version}/megameklab-${version}.tar.gz";
    hash = "sha256-1w70phkwk44hcs1lw20kfhq17lnshjrg8j644v7vdldyfn5djx41";
  };
in
stdenvNoCC.mkDerivation {

I am sorry I don’t have any more good ideas. The rest are some stabs in the dark:

  • try nix-build instead of nix build, something like nix-build --expr '{pkgs ? import <nixpkgs> {}}: pkgs.callPackage ./pkgs/megameklab {}'
  • try to put the callPackage directly into packages in your flake instead in the overlay:
    packages.${system} = {
      megameklab2 = pkgs.callPackage ./pkgs/megameklab {};
    }
    
  • try stdenv instead of stdenvNoCC

Thanks a lot @lucc! I’ve already tried stdenv.mkDerivation with no success. However, I’ve managed to narrow down the problem a bit, by reducing the package code to a minimum.

This works:

{                                                                                                                                                                                                                                                                                                                    
  fetchurl,                                                                                                                                                                                                                                                                                                          
  pkgs,                                                                                                                                                                                                                                                                                                              
}:                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                      
 let                                                                                                                                                                                                                                                                                                                  
   pname = "megameklab";                                                                                                                                                                                                                                                                                              
   version = "0.50.11";                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                      
   src = fetchurl {                                                                                                                                                                                                                                                                                                   
     url = "https://github.com/MegaMek/megameklab/releases/download/v${version}/megameklab-${version}.tar.gz";                                                                                                                                                                                                        
     hash = "sha256-1w70phkwk44hcs1lw20kfhq17lnshjrg8j644v7vdldyfn5djx41";                                                                                                                                                                                                                                            
   };                                                                                                                                                                                                                                                                                                                 
 in                                                                                                                                                                                                                                                                                                                   
 pkgs.stdenv.mkDerivation {                                                                                                                                                                                                                                                                                           
    name = "hello-world";                                                                                                                                                                                                                                                                                              
    # inherit src;                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                      
   buildCommand = ''                                                                                                                                                                                                                                                                                                  
     echo '#!/bin/bash' > $out                                                                                                                                                                                                                                                                                        
     echo 'echo "Hello, World!"' >> $out                                                                                                                                                                                                                                                                              
     chmod +x $out                                                                                                                                                                                                                                                                                                    
   '';                                                                                                                                                                                                                                                                                                                
}

But it stops working as soon as I uncomment inherit src (i.e. it aborts with exit code 1 and no error message). I’ve replaced fetchurl with fetchFromGithub but that didn’t change anything.

I’ve also tried to change the hash, only to provoke at least some kind or error, but that also didn’t change anything. Only if I remove the sha256 prefix, it actually prints an error (about the missing hash type). Weird…

It is strange that you aren’t getting a more helpful error message, but I think at least part of your issue is that your hash is formatted incorrectly. It should be sha256-7Ac3pGw8c7yqhEocFTTXh/ORbiBp9tzGmDXr5wfizh8=.

Edit: Ah, this is probably Silent failure on invalid hash string · Issue #14236 · NixOS/nix · GitHub. What is the result of nix-env --version for you?

1 Like

OMFG, that’s it! Thank you @rhendric! After fixing the hash, it finally works (well, the build at least, the app seems to be missing some dependencies). I really hope that the Nix build system isn’t always that fragile :crossed_fingers:

✦ ➜ nix-env --version
nix-env (Nix) 2.31.2

Btw, which way would you recommend for determining the correct hash? nix-prefetch-url --unpack seems to be the wrong choice…

Just leave the hash as an empty string; nix will scream the correct value at you. It goes AAAAAAAAAAAAAAAAAAA and everything.

2 Likes