Skip to content

Commit

Permalink
More control over the build process
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloudef committed Jan 16, 2024
1 parent 9a5ea43 commit 2af2c0e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
17 changes: 11 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@
runtimeForTargetSystem = args': let
system = if isString args' then zig2nix-lib.mkZigSystemFromString args' else args';
targetPkgs = pkgsForTarget system;
env = {
env = rec {
linux = {
LIBRARY_PATH = "LD_LIBRARY_PATH";
nativeBuildInputs = [ pkgs.autoPatchelfHook ];
wrapperBuildInputs = [ pkgs.autoPatchelfHook ];
};
darwin = let
sdkVer = targetPkgs.targetPlatform.darwinSdkVersion;
Expand All @@ -112,6 +112,9 @@
LIBRARY_PATH = "DYLD_LIBRARY_PATH";
stdenvZigFlags = [ "--sysroot" sdk ];
};
ios = darwin;
watchos = darwin;
tvos = darwin;
};
libs = {
linux = with targetPkgs; []
Expand Down Expand Up @@ -378,10 +381,12 @@
(cd templates/"$var"; nix run --override-input zig2nix ../.. .#test)
printf -- 'build . (%s)\n' "$var"
(cd templates/"$var"; nix build --override-input zig2nix ../.. .; ./result/bin/"$var")
for arch in x86_64-windows ${concatStringsSep " " lib.systems.flakeExposed}; do
printf -- 'build .#target.%s (%s)\n' "$arch" "$var"
(cd templates/"$var"; nix build --override-input zig2nix ../.. .#target."$arch"; file ./result/bin/"$var"*)
done
if [[ "$var" == master ]]; then
for arch in x86_64-windows ${concatStringsSep " " lib.systems.flakeExposed}; do
printf -- 'build .#target.%s (%s)\n' "$arch" "$var"
(cd templates/"$var"; nix build --override-input zig2nix ../.. .#target."$arch"; file ./result/bin/"$var"*)
done
fi
rm -f templates/"$var"/result
rm -rf templates/"$var"/zig-out
rm -rf templates/"$var"/zig-cache
Expand Down
3 changes: 2 additions & 1 deletion package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ let
in stdenvNoCC.mkDerivation (
(removeAttrs attrs [ "stdenvNoCC" ]) // {
zigBuildFlags = (attrs.zigBuildFlags or default-flags) ++ [ "-Dtarget=${target-triple}" ] ++ stdenv-flags;
nativeBuildInputs = [ zig.hook makeWrapper ]
nativeBuildInputs = [ zig.hook ]
++ optionals (!zigDisableWrap) ([ makeWrapper ] ++ (runtime.env.wrapperBuildInputs or []))
++ (runtime.env.nativeBuildInputs or [])
++ (attrs.nativeBuildInputs or []);
postPatch = optionalString (pathExists zigBuildZonLock) ''
Expand Down
27 changes: 16 additions & 11 deletions templates/default/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,37 @@
# Check the flake.nix in zig2nix project for more options:
# <https://github.com/Cloudef/zig2nix/blob/master/flake.nix>
env = zig2nix.outputs.zig-env.${system} {};
doubles = env.pkgs.lib.systems.doubles.all ++ [ "aarch64-ios" ];
in with builtins; with env.pkgs.lib; rec {
# nix build .#target.{nix-target}
# e.g. nix build .#target.x86_64-linux
packages.target = genAttrs systems.doubles.all (target: env.packageForTarget target ({
packages.target = genAttrs doubles (target: env.packageForTarget target ({
src = ./.;
nativeBuildInputs = with env.pkgs; [];
buildInputs = with env.pkgsForTarget target; [];

# Smaller binaries and avoids shipping glibc.
zigPreferMusl = true;

# This disables LD_LIBRARY_PATH mangling, binary patching etc...
# The package won't be usable inside nix.
zigDisableWrap = true;
} // optionalAttrs (!pathExists ./build.zig.zon) {
pname = "my-zig-project";
version = "0.0.0";
}));

# nix build .
packages.default = packages.target.${system};
packages.default = packages.target.${system}.override {
# Prefer nix friendly settings.
zigPreferMusl = false;
zigDisableWrap = false;
};

# For bundling with nix bundle for running outside of nix
# example: https://github.com/ralismark/nix-appimage
apps.bundle.target = genAttrs systems.doubles.all (target: let
pkg = packages.target.${target}.override {
# This disables LD_LIBRARY_PATH mangling.
# vulkan-loader, x11, wayland, etc... won't be included in the bundle.
zigDisableWrap = true;

# Smaller binaries and avoids shipping glibc.
zigPreferMusl = true;
};
apps.bundle.target = genAttrs doubles (target: let
pkg = packages.target.${target};
in {
type = "app";
program = "${pkg}/bin/default";
Expand Down
27 changes: 16 additions & 11 deletions templates/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,37 @@
# Check the flake.nix in zig2nix project for more options:
# <https://github.com/Cloudef/zig2nix/blob/master/flake.nix>
env = zig2nix.outputs.zig-env.${system} {/*SED_ZIG_VER*/};
doubles = env.pkgs.lib.systems.doubles.all ++ [ "aarch64-ios" ];
in with builtins; with env.pkgs.lib; rec {
# nix build .#target.{nix-target}
# e.g. nix build .#target.x86_64-linux
packages.target = genAttrs systems.doubles.all (target: env.packageForTarget target ({
packages.target = genAttrs doubles (target: env.packageForTarget target ({
src = ./.;
nativeBuildInputs = with env.pkgs; [];
buildInputs = with env.pkgsForTarget target; [];

# Smaller binaries and avoids shipping glibc.
zigPreferMusl = true;

# This disables LD_LIBRARY_PATH mangling, binary patching etc...
# The package won't be usable inside nix.
zigDisableWrap = true;
} // optionalAttrs (!pathExists ./build.zig.zon) {
pname = "my-zig-project";
version = "0.0.0";
}));

# nix build .
packages.default = packages.target.${system};
packages.default = packages.target.${system}.override {
# Prefer nix friendly settings.
zigPreferMusl = false;
zigDisableWrap = false;
};

# For bundling with nix bundle for running outside of nix
# example: https://github.com/ralismark/nix-appimage
apps.bundle.target = genAttrs systems.doubles.all (target: let
pkg = packages.target.${target}.override {
# This disables LD_LIBRARY_PATH mangling.
# vulkan-loader, x11, wayland, etc... won't be included in the bundle.
zigDisableWrap = true;

# Smaller binaries and avoids shipping glibc.
zigPreferMusl = true;
};
apps.bundle.target = genAttrs doubles (target: let
pkg = packages.target.${target};
in {
type = "app";
program = "${pkg}/bin/@SED_ZIG_BIN@";
Expand Down
27 changes: 16 additions & 11 deletions templates/master/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,37 @@
# Check the flake.nix in zig2nix project for more options:
# <https://github.com/Cloudef/zig2nix/blob/master/flake.nix>
env = zig2nix.outputs.zig-env.${system} { zig = zig2nix.outputs.packages.${system}.zig.master; };
doubles = env.pkgs.lib.systems.doubles.all ++ [ "aarch64-ios" ];
in with builtins; with env.pkgs.lib; rec {
# nix build .#target.{nix-target}
# e.g. nix build .#target.x86_64-linux
packages.target = genAttrs systems.doubles.all (target: env.packageForTarget target ({
packages.target = genAttrs doubles (target: env.packageForTarget target ({
src = ./.;
nativeBuildInputs = with env.pkgs; [];
buildInputs = with env.pkgsForTarget target; [];

# Smaller binaries and avoids shipping glibc.
zigPreferMusl = true;

# This disables LD_LIBRARY_PATH mangling, binary patching etc...
# The package won't be usable inside nix.
zigDisableWrap = true;
} // optionalAttrs (!pathExists ./build.zig.zon) {
pname = "my-zig-project";
version = "0.0.0";
}));

# nix build .
packages.default = packages.target.${system};
packages.default = packages.target.${system}.override {
# Prefer nix friendly settings.
zigPreferMusl = false;
zigDisableWrap = false;
};

# For bundling with nix bundle for running outside of nix
# example: https://github.com/ralismark/nix-appimage
apps.bundle.target = genAttrs systems.doubles.all (target: let
pkg = packages.target.${target}.override {
# This disables LD_LIBRARY_PATH mangling.
# vulkan-loader, x11, wayland, etc... won't be included in the bundle.
zigDisableWrap = true;

# Smaller binaries and avoids shipping glibc.
zigPreferMusl = true;
};
apps.bundle.target = genAttrs doubles (target: let
pkg = packages.target.${target};
in {
type = "app";
program = "${pkg}/bin/master";
Expand Down

0 comments on commit 2af2c0e

Please sign in to comment.