From 2af2c0e2cf41d6177a23ff98192132fa72e10d93 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Tue, 16 Jan 2024 17:28:05 +0900 Subject: [PATCH] More control over the build process --- flake.nix | 17 +++++++++++------ package.nix | 3 ++- templates/default/flake.nix | 27 ++++++++++++++++----------- templates/flake.nix | 27 ++++++++++++++++----------- templates/master/flake.nix | 27 ++++++++++++++++----------- 5 files changed, 61 insertions(+), 40 deletions(-) diff --git a/flake.nix b/flake.nix index a9c8165..8944c14 100644 --- a/flake.nix +++ b/flake.nix @@ -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; @@ -112,6 +112,9 @@ LIBRARY_PATH = "DYLD_LIBRARY_PATH"; stdenvZigFlags = [ "--sysroot" sdk ]; }; + ios = darwin; + watchos = darwin; + tvos = darwin; }; libs = { linux = with targetPkgs; [] @@ -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 diff --git a/package.nix b/package.nix index b8e8852..1ff2927 100644 --- a/package.nix +++ b/package.nix @@ -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) '' diff --git a/templates/default/flake.nix b/templates/default/flake.nix index c0fa92d..438dc31 100644 --- a/templates/default/flake.nix +++ b/templates/default/flake.nix @@ -12,32 +12,37 @@ # Check the flake.nix in zig2nix project for more options: # 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"; diff --git a/templates/flake.nix b/templates/flake.nix index fc42282..234be6c 100644 --- a/templates/flake.nix +++ b/templates/flake.nix @@ -12,32 +12,37 @@ # Check the flake.nix in zig2nix project for more options: # 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@"; diff --git a/templates/master/flake.nix b/templates/master/flake.nix index ad58683..a81ff35 100644 --- a/templates/master/flake.nix +++ b/templates/master/flake.nix @@ -12,32 +12,37 @@ # Check the flake.nix in zig2nix project for more options: # 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";