Using the `src` attribute in `runCommand`

I’m a bit confused by this:

pkgs.runCommand "build-cv" {
  buildInputs = with pkgs; [
    texlive.combined.scheme-context
  ];
  src = sourceFilesBySuffices self [".tex"];
} ''
  # Oddly, the "src" property doesn't seem to work in runCommand?
  cp -r "$src/"* .
  context cv
  mkdir -p $out
  cp cv.pdf $out/cv.pdf
'';

As far as I can tell from the runCommand source, src should just be passed directly to stdenv.mkDerivation, which usually just brings up my build shell in a directory with my sources available.

Is this a quirk of stdenvNoCC? Is there something else I misunderstand?

It also sets buildCommand, and setting buildCommand overrides all the phases in a stdenv derivation. So because a custom buildCommand is set, unpackPhase is never run.

If you want src to work, the best thing is probably just to use stdenv.mkDerivation and set a custom buildPhase and installPhase.

1 Like

D’oh, of course this overrides the actual behavior, rather than using the phases. Heh, I’ve been scratching my head over this for 30 minutes now. Thanks!

For reference:

pkgs.stdenvNoCC.mkDerivation {
  name = "cv";
  src = sourceFilesBySuffices self [".tex"];

  buildInputs = with pkgs; [
    texlive.combined.scheme-context
  ];

  OSFONTDIR =
    concatMapStringsSep ";" (pkg: "${pkg}/share/fonts/truetype/")
    (with pkgs; [
      roboto
      roboto-slab
      roboto-mono
    ]);

  buildPhase = ''
    context cv
  '';

  installPhase = ''
    mkdir -p $out
    cp cv.pdf $out/cv.pdf
  '';
}

First time I’ve done this in a while, so I’d like to point out that using nix as a build system for tex is (perhaps unsurprisingly) very nice, and not a use case I’d considered before. First time in a while that I feel like my pdf will still build in a year or two :slight_smile:

1 Like

A recent post on building LaTeX documents: Exploring Nix Flakes: Build LaTeX Documents Reproducibly.

1 Like

For adding flake sources to the Nix store I use this when I want to intentionally avoid a patchPhase and fixupPhase. It’s essentially the unrolled version of runCommandNoCC but with a more limited closure size on the input derivations.

It runs noticeably faster on large numbers of small sources as well because setup.sh isn’t processed.

There’s also an example for placing files in subdirs in there as well.

Hm, interesting. I use the source helpers because they avoid my output depending on the .git directory; I don’t see how your approach achieves that. Is there magic involved I’m unaware of?