Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial buildZigPackage #241741

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/languages-frameworks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ swift.section.md
texlive.section.md
titanium.section.md
vim.section.md
zig.section.md
```
35 changes: 35 additions & 0 deletions doc/languages-frameworks/zig.section.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Zig {#zig}

## Overview {#zig-overview}

The Zig compiler and a builder function are available in Nixpkgs.

## Zig program packages in Nixpkgs {#zig-program-packages-in-nixpkgs}

Zig programs can be built using `buildZigPackage`. This builder function is available under any Zig compiler version.

The following example shows a Zig program. It uses Zig 0.9.
`buildZigPackage` *always* uses `-Drelease-safe -Dcpu=baseline` flags.

```nix
{ lib, fetchFromGitHub, zig_0_9 }:

zig_0_9.buildZigPackage rec {
pname = "colorstorm";
version = "2.0.0";

src = fetchFromGitHub {
owner = "benbusby";
repo = "colorstorm";
rev = "v${version}";
hash = "sha256-6+P+QQpP1jxsydqhVrZkjl1gaqNcx4kS2994hOBhtu8=";
};
}
```

## `buildZigPackage` parameters {#buildzigpackage-parameters}

All parameters from `stdenv.mkDerivation` function are still supported. The
following are specific to `buildZigPackage`:

* `zigBuildFlags`: A list of strings holding the build flags passed to Zig compiler. By default it is empty.
20 changes: 20 additions & 0 deletions pkgs/applications/misc/colorstorm/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{ lib, fetchFromGitHub, zig_0_9 }:

zig_0_9.buildZigPackage rec {
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
pname = "colorstorm";
version = "2.0.0";

src = fetchFromGitHub {
owner = "benbusby";
repo = "colorstorm";
rev = "v${version}";
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
hash = "sha256-6+P+QQpP1jxsydqhVrZkjl1gaqNcx4kS2994hOBhtu8=";
};

meta = {
description = "A color theme generator for editors and terminal emulators";
homepage = "https://github.com/benbusby/colorstorm";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ ];
};
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
}
16 changes: 5 additions & 11 deletions pkgs/applications/misc/waylock/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
, libxkbcommon
, pam
}:
stdenv.mkDerivation rec {

zig.buildZigPackage rec {
pname = "waylock";
version = "0.6.2";

Expand All @@ -21,7 +22,7 @@ stdenv.mkDerivation rec {
fetchSubmodules = true;
};

nativeBuildInputs = [ zig wayland scdoc pkg-config ];
nativeBuildInputs = [ wayland scdoc pkg-config ];

buildInputs = [
wayland-protocols
Expand All @@ -30,16 +31,9 @@ stdenv.mkDerivation rec {
];

dontConfigure = true;
strictDeps = false;

preBuild = ''
export HOME=$TMPDIR
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
'';

installPhase = ''
runHook preInstall
zig build -Drelease-safe -Dman-pages -Dcpu=baseline --prefix $out install
runHook postInstall
'';
zigBuildFlags = [ "-Dman-pages" ];

meta = with lib; {
homepage = "https://github.com/ifreund/waylock";
Expand Down
15 changes: 7 additions & 8 deletions pkgs/applications/window-managers/river/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{ lib
, stdenv
, fetchFromGitHub
, zig
, wayland
Expand All @@ -18,7 +17,7 @@
, xwaylandSupport ? true
}:

stdenv.mkDerivation rec {
zig.buildZigPackage rec {
pname = "river";
version = "0.2.4";

Expand All @@ -30,7 +29,7 @@ stdenv.mkDerivation rec {
fetchSubmodules = true;
};

nativeBuildInputs = [ zig wayland xwayland scdoc pkg-config ];
nativeBuildInputs = [ wayland xwayland scdoc pkg-config ];

buildInputs = [
wayland-protocols
Expand All @@ -44,16 +43,16 @@ stdenv.mkDerivation rec {
] ++ lib.optional xwaylandSupport libX11;

dontConfigure = true;
strictDeps = false; # This needs to be disabled for the build to work.

preBuild = ''
export HOME=$TMPDIR
'';

installPhase = ''
runHook preInstall
zig build -Drelease-safe -Dcpu=baseline ${lib.optionalString xwaylandSupport "-Dxwayland"} -Dman-pages --prefix $out install
zigBuildFlags = [ "-Dman-pages" ] ++ lib.optional xwaylandSupport "-Dxwayland";

postInstall = ''
install contrib/river.desktop -Dt $out/share/wayland-sessions
runHook postInstall
'';

/* Builder patch install dir into river to get default config
Expand All @@ -62,7 +61,7 @@ stdenv.mkDerivation rec {
*/
installFlags = [ "DESTDIR=$(out)" ];

passthru.providedSessions = ["river"];
passthru.providedSessions = [ "river" ];

meta = with lib; {
changelog = "https://github.com/ifreund/river/releases/tag/v${version}";
Expand Down
44 changes: 44 additions & 0 deletions pkgs/build-support/zig/build-zig-package/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{ lib, stdenv, zig }:
argsFun:

let
wrapDerivation = f:
stdenv.mkDerivation (finalAttrs:
f (lib.toFunction argsFun finalAttrs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you change f to be an overlay, the composition here becomes a lot like lib.extends, except the first layer doesn't take a prev argument.
I think we could use a helper for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use a helper for this.

Actually just lib.extends.
Change f to take finalAttrs: prevAttrs: and then:

-    stdenv.mkDerivation (finalAttrs:
-      f (lib.toFunction argsFun finalAttrs)
+    stdenv.mkDerivation (lib.extends f (lib.toFunction argsFun))

);
in
wrapDerivation (
Comment on lines +5 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
wrapDerivation = f:
stdenv.mkDerivation (finalAttrs:
f (lib.toFunction argsFun finalAttrs)
);
in
wrapDerivation (
changeDerivationArgs = f:
stdenv.mkDerivation (finalAttrs:
f (lib.toFunction argsFun finalAttrs)
);
in
changeDerivationArgs (

The derivation itself isn't wrapped (which is good).

{ strictDeps ? true
, nativeBuildInputs ? [ ]
, meta ? { }
, ...
}@attrs:

attrs // {
inherit strictDeps;
nativeBuildInputs = [ zig ] ++ nativeBuildInputs;

buildPhase = attrs.buildPhase or ''
runHook preBuild
export ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
zig build -Drelease-safe -Dcpu=baseline $zigBuildFlags
runHook postBuild
'';

checkPhase = attrs.checkPhase or ''
runHook preCheck
zig build test
runHook postCheck
'';

installPhase = attrs.installPhase or ''
runHook preInstall
zig build -Drelease-safe -Dcpu=baseline $zigBuildFlags --prefix $out install
runHook postInstall
'';

meta = {
inherit (zig.meta) platforms;
} // meta;
}
Comment on lines +11 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has the shape of an overlay, if you bring finalAttrs into scope.

  • this will be helpful if you have to need the overlay to be aware of the final attrs (at some point)
  • you can document it as such

Specifically you can then say:

buildZigPackage invokes mkDerivation with your package definition and then overlays it, to add its defaults.
Hence the layers are applied in the following order:

  1. your argument to buildZigPackage
  2. zig defaults
  3. anything changed afterwards with overrideAttrs
  4. mkDerivation logic, such as the addition of finalAttrs.finalPackage

)
5 changes: 5 additions & 0 deletions pkgs/development/compilers/zig/0.10.nix
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ stdenv.mkDerivation (finalAttrs: {
runHook postInstallCheck
'';

passthru.buildZigPackage = import ../../../build-support/zig/build-zig-package/default.nix {
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
inherit lib stdenv;
zig = finalAttrs.finalPackage;
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
};

meta = {
homepage = "https://ziglang.org/";
description =
Expand Down
5 changes: 5 additions & 0 deletions pkgs/development/compilers/zig/0.9.1.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ stdenv.mkDerivation (finalAttrs: {
runHook postCheck
'';

passthru.buildZigPackage = import ../../../build-support/zig/build-zig-package/default.nix {
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
IogaMaster marked this conversation as resolved.
Show resolved Hide resolved
inherit lib stdenv;
zig = finalAttrs.finalPackage;
};

meta = {
homepage = "https://ziglang.org/";
description =
Expand Down
31 changes: 2 additions & 29 deletions pkgs/tools/audio/linuxwave/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
, zig
}:

stdenv.mkDerivation rec {
zig.buildZigPackage rec {
pname = "linuxwave";
version = "0.1.4";

Expand All @@ -19,37 +19,10 @@ stdenv.mkDerivation rec {

nativeBuildInputs = [
installShellFiles
zig
];

postConfigure = ''
export XDG_CACHE_HOME=$(mktemp -d)
'';

buildPhase = ''
runHook preBuild

zig build -Drelease-safe -Dcpu=baseline

runHook postBuild
'';

checkPhase = ''
runHook preCheck

zig build test

runHook postCheck
'';

installPhase = ''
runHook preInstall

zig build -Drelease-safe -Dcpu=baseline --prefix $out install

postInstall = ''
installManPage man/linuxwave.1

runHook postInstall
'';

meta = with lib; {
Expand Down
13 changes: 1 addition & 12 deletions pkgs/tools/misc/ncdu/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib, stdenv, fetchurl, zig, ncurses }:

stdenv.mkDerivation rec {
zig.buildZigPackage rec {
pname = "ncdu";
version = "2.2.2";

Expand All @@ -9,19 +9,8 @@ stdenv.mkDerivation rec {
hash = "sha256-kNkgAk51Ixi0aXds5X4Ds8cC1JMprZglruqzbDur+ZM=";
};

XDG_CACHE_HOME="Cache"; # FIXME This should be set in stdenv

nativeBuildInputs = [
zig
];

buildInputs = [ ncurses ];

PREFIX = placeholder "out";

# Avoid CPU feature impurity, see https://github.com/NixOS/nixpkgs/issues/169461
ZIG_FLAGS = "-Drelease-safe -Dcpu=baseline";

meta = with lib; {
description = "Disk usage analyzer with an ncurses interface";
homepage = "https://dev.yorhel.nl/ncdu";
Expand Down
2 changes: 2 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ with pkgs;

colorpicker = callPackage ../tools/misc/colorpicker { };

colorstorm = callPackage ../applications/misc/colorstorm { };

comedilib = callPackage ../development/libraries/comedilib { };

commix = callPackage ../tools/security/commix { };
Expand Down