Help making a flake (for Geant4 (particle physics framework) examples)


Can you help me make a semi-decent flake for running examples of the Geant4 toolkit for interaction of particles with matter?

Here is what I have so far: GitHub - jacg/g4-examples-flake: Exploring Geant4 examples with Nix flakes.


  1. This is a Nix flake, therefore it won’t work without a flakes-enabled (i.e. unstable) Nix.

  2. I don’t know how to get Geant4 to work in Nix on MacOS, so this only works on Linux.

Something to try, that should work

nix build github:jacg/g4-examples-flake --out-link /tmp/g4b1
tree /tmp/g4b1 # (assuming you have `tree`)
nix develop
/tmp/g4b1/bin/exampleB1 # Tries to use OpenGL so YMMV

Things that don’t work

  1. The executable absolutely needs to know the location of the G4 datasets (and probably some other crucial info). This is done by sourcing ${geant4}/bin/, which seems to happen automatically in nix develop (I’m not sure how), but I don’t know how to make that happen for nix shell or nix run.

  2. G4 programs are supposed to be executed from the directory containing the *.mac files which control the run. In this flake, that’s B1_MACRO_DIR. I don’t know how to get nix shell and nix run to cd B1_MACRO_DIR.

Consequently nix shell and nix run don’t do anything sensible. The latter does demonstrate the need to know the aforementioned G4 data directories, by crashing with the error

G4ENSDFSTATEDATA environment variable must be set
*** Fatal Exception *** core dump ***



  • Can you spot anything obviously stupid/superstitious/misguided in what I have so far?

  • I never remember/understand the distinction between buildInputs and nativeBuildInputs. Can you offer any wisdom?

  • Is there any documentation for defaultApp anywhere? From examples I infer that { type = "app"; program = "<an executable>"; } works, but can you do anything else with it? What types are there besides "app"? [Answered below; TLDR: nope, that’s it!]

  • Can you suggest how to make progress on getting nix shell and nix run?

1 Like

This seems to be it: nix/ at 1b578255245e2e1347059ad7d9171cf822c723a8 · NixOS/nix · GitHub

At present, the answer seems to be ‘no’.

At present, the answer seems to be ‘none at all’.

1 Like


  1. The old nix-shell served a dual purpose and the new Nix caters for these via two separate mechanisms: nix shell and nix develop.

  2. In the old Nix, shellHook was executed by nix-shell.

What are the interactions between shellHook and nix shell/nix develop?

In my flake, nix develop shows signs of having executed source ${pkgs.geant4}/nix-support/setup-hook but I have no idea how or why; various experiments seem to suggest that it has nothing to do with shellHook.

The effects of source ${pkgs.geant4}/nix-support/setup-hook are absent from (but needed in) nix shell: how should I go about providing them?

Maybe this should be done with wrapProgram? But I don’t see how to automate extraction of the settings provided by ${geant4}/nix-support/setup-hook into a form that can be consumed by wrapProgram.


Since nix develop is the successor of nix-shell; while not documented, it has the same behaviour regarding shellHook.

nix-shell and nix develop are used to create the build environment of a derivation. shellHook can be used to perform initialisation specific to the interactive build environment. So it is only useful for nix-shell and nix develop.

nix shell, similar to nix-shell -p, is used to create a shell environment where the specified packages are already built and ready to be used. So shellHook variable isn’t used with nix shell and nix-shell -p.