mirror of
https://github.com/ziglang/zig.git
synced 2024-11-21 11:32:24 +00:00
Page:
Development with nix
Pages
Building Zig From Source
Building Zig on Windows
Community Projects
Community
Contributing
Development with nix
FAQ
Glossary
Home
How to build LLVM, libclang, and liblld from source
How to read the standard library source code
Install Zig from a Package Manager
LLVM Upgrade Process
Language Proposals
Self Hosted Compiler Upgrade Guide
Setting up a new x86_64 Linux CI server
Setting up an ARM64 Windows Dev Kit to be a CI Runner
Third Party Tracking Issues (what is important to other people?)
Troubleshooting Build Issues
Updating libc
Using Cast Result Type Inference
Why Zig When There is Already CPP, D, and Rust?
_Build
zig cc compatibility with clang
26
Development with nix
Motiejus Jakštys edited this page 2024-08-27 16:01:18 +03:00
Table of Contents
nix-zig-build
A Nix Flake for building and testing Zig.
https://github.com/erikarvstedt/nix-zig-build
Traditional approach
Here is a sample shell.nix
for building/developing zig.
with import <nixpkgs> {};
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
cmake
gdb
libxml2
ninja
qemu
wasmtime
zlib
] ++ (with llvmPackages_17; [
clang
clang-unwrapped
lld
llvm
]);
hardeningDisable = [ "all" ];
}
The hardeningDisable
part is crucial, otherwise you will get compile errors.
Flake
Alternatively, you can use this sample flake.nix
:
{
description = "A flake for Zig development";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = inputs@{ self, ... }: inputs.flake-utils.lib.eachDefaultSystem (system:
let
pkgs = inputs.nixpkgs.legacyPackages.${system};
in
{
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
cmake
gdb
libxml2
ninja
qemu
wasmtime
zlib
] ++ (with llvmPackages_18; [
clang
clang-unwrapped
lld
llvm
]);
hardeningDisable = [ "all" ];
};
# For compatibility with older versions of the `nix` binary
devShell = self.devShells.${system}.default;
packages.default = pkgs.stdenv.mkDerivation {
pname = "zig";
# TODO: Fix the output of `zig version`.
version = "0.10.0-dev";
src = self;
nativeBuildInputs = with pkgs; [
cmake
] ++ (with llvmPackages_14; [
libclang
lld
llvm
]);
preBuild = ''
export HOME=$TMPDIR;
'';
cmakeFlags = [
# https://github.com/ziglang/zig/issues/12069
"-DZIG_STATIC_ZLIB=on"
];
};
}
);
}
Notes for macOS users
If using macOS, you will need to build llvm yourself and use the system's clang to build zig. This is possible using the sample shell.nix
below.
with import <nixpkgs> { };
pkgs.mkShellNoCC {
nativeBuildInputs = with pkgs; [ cmake gdb ninja qemu wasmtime ];
hardeningDisable = [ "all" ];
}