Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Created September 25, 2023 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8henrie/ead88a1112d9c18310f9f971bd4bca87 to your computer and use it in GitHub Desktop.
Save n8henrie/ead88a1112d9c18310f9f971bd4bca87 to your computer and use it in GitHub Desktop.
How to use darwin-builder to build linux nix packages on darwin
{
description = "Example use of nixpkgs#darwin-builder";
# First, read through https://github.com/NixOS/nixpkgs/blob/master/doc/builders/special/darwin-builder.section.md#sec-darwin-builder-example-flake
# and add yourself to `extra-trusted-users`, verify with `nix show-config | grep -i trusted-users`
#
# Copy this flake somewhere and run:
#
# ```console
# $ nix build --builders '' .#packages.aarch64-darwin.default
# $ ./result
# foo
# ```
#
# Works fine. Now try building the aarch64-linux output, specifying
# `--builders ''` to ensure no extra builders try to help.
#
# ```console
# $ nix build --builders '' .#packages.aarch64-linux.default
# error: builder for '/nix/store/44w3vqp3m28l7sygvdfhgg3l36zama57-foo.drv' failed with exit code 126;
# last 1 log lines:
# > /nix/store/nyw3r7n9rr79jvqhdjcvccfqy7rw0lws-bash-5.2-p15/bin/bash: /nix/store/nyw3r7n9rr79jvqhdjcvccfqy7rw0lws-bash-5.2-p15/bin/bash: cannot execute binary file
# For full logs, run 'nix log /nix/store/44w3vqp3m28l7sygvdfhgg3l36zama57-foo.drv'.
# ```
#
# Now run this command in a separate terminal (port 22 must be free):
# `nix run github:nixos/nixpkgs/nixpkgs-23.05-darwin#darwin.builder`
#
# You'll be prompted for your sudo password to install the SSH key and
# eventually get a login prompt.
#
# Once you see a login prompt, return to the other terminal and run the
# `aarch64-linux` command again, but specifying your new builder vm:
#
# ```console
# $ nix build --builders 'builder@localhost aarch64-linux /etc/nix/builder_ed25519' .#packages.aarch64-linux.default
# $ echo $?
# 0
# ```
#
# Sweet!
outputs = {
self,
nixpkgs,
}: {
packages = nixpkgs.lib.genAttrs ["aarch64-darwin" "aarch64-linux"] (system: let
pkgs = import nixpkgs {inherit system;};
in {
default = pkgs.stdenvNoCC.mkDerivation {
name = "foo";
dontUnpack = true;
dontFixup = true;
installPhase = ''
echo '#!/usr/bin/env bash' > $out
echo 'echo "foo"' >> $out
chmod +x $out
'';
};
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment