Help packaging a flake: `nix build` doesn't work, but `nix develop` and `genericBuild` does

I’m attempting to add the apple-emoji-linux font to my personal flake repo (not Nixpkgs because of potential legal issues). Now, I’ve already managed to get a “binary” version of the package working: it gets the release from GitHub and unpacks it and that’s it…and that works fine—but I’d like to also build it from source, because why not?

My flake.nix looks like:

{
  description = "fum's various flakes that aren't good enough to upstream to nixpkgs";

  # i want to depend on the system's nixpkgs? not sure if this works
  # inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

  outputs =
    { nixpkgs, ... }:
    let
      # ...instead of flake-utils
      # https://isabelroses.com/blog/im-not-mad-im-disappointed-10

      forAllSystems =
        function:
        nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed (
          system: function nixpkgs.legacyPackages.${system}
        );
    in
    {
      packages = forAllSystems (pkgs: {
        apple-color-emoji = pkgs.callPackage ./fonts/apple-color-emoji.nix { };
        apple-color-emoji-src = pkgs.callPackage ./fonts/apple-color-emoji-src.nix { };
      });
    };
}

And fonts/apple-color-emoji.nix looks like:

{ pkgs, ... }:

pkgs.stdenvNoCC.mkDerivation rec {
  pname = "apple-color-emoji";
  version = "17.4";
  dontUnpack = true;

  src = pkgs.fetchurl {
    url = "https://github.com/samuelngs/apple-emoji-linux/releases/download/v${version}/AppleColorEmoji.ttf";
    hash = "sha256-SG3JQLybhY/fMX+XqmB/BKhQSBB0N1VRqa+H6laVUPE=";
  };

  installPhase = ''
    mkdir -p $out/share/fonts/truetype
    cp $src $out/share/fonts/truetype
  '';

  meta = {
    description = "A derivation for the Apple Color Emoji font";
  };
}

Running nix build .#apple-color-emoji makes a result symlink, just like you’d expect, with a .ttf in result/share/fonts/truetype.

apple-color-emoji-src.nix, however, looks like:

{
  stdenv,
  fetchFromGitHub,
  python3,
  optipng,
  zopfli,
  pngquant,
  imagemagick,
}:

stdenv.mkDerivation rec {
  pname = "apple-color-emoji-src";
  version = "17.4";

  src = fetchFromGitHub {
    owner = "samuelngs";
    repo = "apple-emoji-linux";
    tag = "v${version}";
    hash = "sha256-liklPjOJhHOBWQH8AQwkLfIG0KIqdnZcVAa7oMrVZMk=";
  };

  # enableParallelBuilding = true;

  nativeBuildInputs = [
    (python3.withPackages (
      python-pkgs: with python-pkgs; [
        fonttools
        nototools
      ]
    ))
    optipng
    zopfli
    pngquant
    imagemagick
  ];

  installPhase = ''
    runHook preInstall
    mkdir -p $out/share/fonts/truetype
    cp ./AppleColorEmoji.ttf $out/share/fonts/truetype
    runHook postInstall
  '';

  meta = {
    description = "A derivation for the Apple Color Emoji font";
  };
}

Attempting to nix build .#apple-color-emoji-src fails because it can’t find zopflipng:

$ nix build .\#apple-color-emoji-src
warning: Git tree '/home/fumnanya/Downloads/flakes' is dirty
warning: updating lock file '/home/fumnanya/Downloads/flakes/flake.lock':
• Added input 'nixpkgs':
    'path:/nix/store/593xvgv994xlkm5mb7w4p1xxnzrs9wv6-source?lastModified=0&narHash=sha256-WGaHVAjcrv%2BCun7zPlI41SerRtfknGQap281%2BAakSAw%3D' (1970-01-01)
warning: Git tree '/home/fumnanya/Downloads/flakes' is dirty
error: builder for '/nix/store/sv5vp8r2342x0kv5pfkam7lvnk7b2s3x-apple-color-emoji-src-17.4.drv' failed with exit code 2;
       last 15 log lines:
       > Running phase: unpackPhase
       > unpacking source archive /nix/store/pvy9am729cnxvvzg7saxnhvjdpn9vqja-source
       > source root is source
       > Running phase: patchPhase
       > Running phase: updateAutotoolsGnuConfigScriptsPhase
       > Running phase: configurePhase
       > no configure script, doing nothing
       > Running phase: buildPhase
       > build flags: SHELL=/nix/store/11ciq72n4fdv8rw6wgjgasfv4mjs1jrw-bash-5.2p37/bin/bash
       > /nix/store/11ciq72n4fdv8rw6wgjgasfv4mjs1jrw-bash-5.2p37/bin/bash: line 1: which: command not found
       > /nix/store/11ciq72n4fdv8rw6wgjgasfv4mjs1jrw-bash-5.2p37/bin/bash: line 1: which: command not found
       > /nix/store/11ciq72n4fdv8rw6wgjgasfv4mjs1jrw-bash-5.2p37/bin/bash: line 1: which: command not found
       > mkdir -p "build/emoji"
       > mkdir -p "build/quantized_pngs"
       > Makefile:85: *** "neither zopflipng nor optipng is available".  Stop.
       For full logs, run 'nix log /nix/store/sv5vp8r2342x0kv5pfkam7lvnk7b2s3x-apple-color-emoji-src-17.4.drv'.

…which is puzzling, because if we enter the environment and try to build it:

$ nix develop .\#apple-color-emoji-src -i
warning: Git tree '/home/fumnanya/Downloads/flakes' is dirty
warning: updating lock file '/home/fumnanya/Downloads/flakes/flake.lock':
• Added input 'nixpkgs':
    'path:/nix/store/593xvgv994xlkm5mb7w4p1xxnzrs9wv6-source?lastModified=0&narHash=sha256-WGaHVAjcrv%2BCun7zPlI41SerRtfknGQap281%2BAakSAw%3D' (1970-01-01)
warning: Git tree '/home/fumnanya/Downloads/flakes' is dirty
bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory
bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory
bash: warning: setlocale: LC_COLLATE: cannot change locale (en_US.UTF-8): No such file or directory
bash-5.2$ which zopflipng
/nix/store/hcg4p8aj4h5c5kncf03g9lppw6l4qv60-zopfli-1.0.3/bin/zopflipng
bash-5.2$ which python 
/nix/store/077hwqhhq8vlvbvqzwbiznsjp8z52w7k-python3-3.12.9-env/bin/python
bash-5.2$ which convert
/nix/store/jx11ykx0nzqpxmm7f10m0ff25ml05f5a-imagemagick-7.1.1-43/bin/convert
bash-5.2$ genericBuild
Running phase: unpackPhase
unpacking source archive /nix/store/pvy9am729cnxvvzg7saxnhvjdpn9vqja-source
source root is source
Running phase: patchPhase
Running phase: updateAutotoolsGnuConfigScriptsPhase
Running phase: configurePhase
no configure script, doing nothing
Running phase: buildPhase
build flags: SHELL=/nix/store/11ciq72n4fdv8rw6wgjgasfv4mjs1jrw-bash-5.2p37/bin/bash
mkdir -p "build/emoji"
mkdir -p "build/quantized_pngs"
using zopflipng
mkdir -p "build/compressed_pngs"
WARNING: The convert command is deprecated in IMv7, use "magick" instead of "convert" or "magick convert"

WARNING: The convert command is deprecated in IMv7, use "magick" instead of "convert" or "magick convert"

^C

What am I doing wrong? Is there something I’m not setting correctly?

You’re missing which in nativeBuildInputs.
(which isn’t part of stdenv, see https://nixos.org/manual/nixpkgs/stable/#sec-tools-of-stdenv.)

1 Like

i…oh my…damn i thought which was a shell builtin or something lol and just assumed it was failing to find what which was called with (i’m guessing type is what’s always available on every POSIX shell then?)

weird that the -i flag of nix develop didn’t clear it out though, you’d think it would…

(the linked page does say building with nix-shell—and nix develop i assume—may “have some inconsistencies in environment variables and behaviour compared to a normal build within the Nix build sandbox”)