{"id":13709855,"url":"https://github.com/Cloudef/zig2nix","last_synced_at":"2025-05-06T18:33:05.394Z","repository":{"id":216610081,"uuid":"741782669","full_name":"Cloudef/zig2nix","owner":"Cloudef","description":"Flake for packaging, building and running Zig projects.","archived":false,"fork":false,"pushed_at":"2024-10-30T01:31:36.000Z","size":755,"stargazers_count":81,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-30T03:57:59.324Z","etag":null,"topics":["automation","build","ci","cross-compiling","dependencies","developer-tools","nix","packaging","release","releases","reproducible","zig"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Cloudef.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["Cloudef"]}},"created_at":"2024-01-11T05:16:46.000Z","updated_at":"2024-10-30T01:31:40.000Z","dependencies_parsed_at":"2024-03-10T02:38:41.666Z","dependency_job_id":"b0ec3a37-609a-4019-8e0f-4cc667e8be75","html_url":"https://github.com/Cloudef/zig2nix","commit_stats":null,"previous_names":["cloudef/zig2nix"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cloudef%2Fzig2nix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cloudef%2Fzig2nix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cloudef%2Fzig2nix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cloudef%2Fzig2nix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cloudef","download_url":"https://codeload.github.com/Cloudef/zig2nix/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224521401,"owners_count":17325229,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","build","ci","cross-compiling","dependencies","developer-tools","nix","packaging","release","releases","reproducible","zig"],"created_at":"2024-08-02T23:00:47.230Z","updated_at":"2025-05-06T18:33:05.348Z","avatar_url":"https://github.com/Cloudef.png","language":"Zig","readme":"# zig2nix flake\n\nFlake for packaging, building and running Zig projects.\n\nhttps://ziglang.org/\n\n* Cachix: `cachix use zig2nix`\n\n---\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n* Zig master: `0.15.0-dev.465+bb79c85cb @ 2025-05-05`\n* Zig latest: `0.14.0 @ 2025-03-05`\n\n## Examples\n\n### Zig project template\n\n```bash\nnix flake init -t github:Cloudef/zig2nix\nnix run .\n# for more options check the flake.nix file\n```\n\n#### With master version of Zig\n\n```bash\nnix flake init -t github:Cloudef/zig2nix#master\nnix run .\n# for more options check the flake.nix file\n```\n\n### Build zig from source\n\n```bash\nnix build github:Cloudef/zig2nix#zig-src-master\nnix build github:Cloudef/zig2nix#zig-src-latest\nnix build github:Cloudef/zig2nix#zig-src-0_8_0\n```\n\n### Running zig compiler directly\n\n```bash\nnix run github:Cloudef/zig2nix#master -- version\nnix run github:Cloudef/zig2nix#latest -- version\nnix run github:Cloudef/zig2nix#0_8_0 -- version\n```\n\n#### Convenience zig for multimedia programs\n\n\u003e This sets (DY)LD_LIBRARY_PATH and PKG_CONFIG_PATH so that common libs are available\n\n```bash\nnix run github:Cloudef/zig2nix#multimedia-master -- version\nnix run github:Cloudef/zig2nix#multimedia-latest -- version\nnix run github:Cloudef/zig2nix#multimedia-0_8_0 -- version\n```\n\n### Shell for building and running a Zig project\n\n```bash\nnix develop github:Cloudef/zig2nix#master\nnix develop github:Cloudef/zig2nix#latest\nnix develop github:Cloudef/zig2nix#0_8_0\n```\n\n### Convert zon file to json\n\n```bash\nnix run github:Cloudef/zig2nix -- zon2json build.zig.zon\n```\n\n### Convert build.zig.zon to a build.zig.zon2json-lock\n\n```bash\nnix run github:Cloudef/zig2nix -- zon2lock build.zig.zon\n```\n\n### Convert build.zig.zon/2json-lock to a nix derivation\n\n```bash\n# calls zon2json-lock if build.zig.zon2json-lock does not exist (requires network access)\nnix run github:Cloudef/zig2nix -- zon2nix build.zig.zon\n# alternatively run against the lock file (no network access required)\nnix run github:Cloudef/zig2nix -- zon2nix build.zig.zon2json-lock\n```\n\n### Github actions\n\nWhen using zig2nix in github actions, you have to disable apparmor in the ubuntu runner:\n\n```bash\nsudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0\n```\n\n## Crude documentation\n\nBelow is auto-generated dump of important outputs in this flake.\n\n```nix\n#! Structures.\n\n#:! Helper function for building and running Zig projects.\nzig-env = {\n # Overrideable nixpkgs.\n nixpkgs ? self.inputs.nixpkgs,\n # Zig version to use.\n zig ? zigv.latest,\n}: { ... };\n\n#! --- Outputs of zig-env {} function.\n#!     access: (zig-env {}).thing\n\n#! Tools for bridging zig and nix\n#! The correct zig version is put into the PATH\nzig2nix = pkgs.writeShellApplication {\n name = \"zig2nix\";\n runtimeInputs = [ zig ];\n text = ''${zig2nix-zigless}/bin/zig2nix \"$@\"'';\n};\n\n#! Translates zig and nix compatible targets\ntarget = system: (exec-json \"target\" [ system ]);\n\n#! Reads zon file into a attribute set\nfromZON = path: exec-json-path \"zon2json\" path [];\n\n#! Creates derivation from zon2json-lock file\nderiveLockFile = path: pkgs.callPackage (exec-path \"zon2nix\" path [ \"-\" ]);\n\n#! Returns true if target is nix flake compatible.\n#! \u003chttps://github.com/NixOS/nixpkgs/blob/master/lib/systems/flake-systems.nix\u003e\nisFlakeTarget = any: pkgs.lib.any (s: (systems.elaborate s).config == (target any).config) systems.flakeExposed;\n\n#! Returns crossPkgs from nixpkgs for target string or system.\n#! This will always cross-compile the package.\ncrossPkgsForTarget = any: let\n crossPkgs = import nixpkgs { localSystem = system; crossSystem = { config = (target any).config; }; };\n this-system = (systems.elaborate system).config == (target any).config;\nin if this-system then pkgs else crossPkgs;\n\n#! Returns pkgs from nixpkgs for target string or system.\n#! This does not cross-compile and you'll get a error if package does not exist in binary cache.\nbinaryPkgsForTarget = any: let\n binaryPkgs = import nixpkgs { localSystem = { config = (target any).config; }; };\n this-system = (systems.elaborate system).config == (target any).config;\nin if this-system then pkgs else binaryPkgs;\n\n#! Returns either binaryPkgs or crossPkgs depending if the target is flake target or not.\npkgsForTarget = any:\nif isFlakeTarget any then binaryPkgsForTarget any\nelse crossPkgsForTarget any;\n\n#! Cross-compile nixpkgs using zig :)\n#! NOTE: This is an experimental feature, expect it not faring well\nzigCrossPkgsForTarget = any: let\n crossPkgs = pkgs.callPackage ./src/cross {\n  inherit zig zigPackage target;\n  nixCrossPkgs = pkgsForTarget any;\n  nixBinaryPkgs = binaryPkgsForTarget any;\n  localSystem = system;\n  crossSystem = { config = (target any).config; };\n };\nin warn \"zigCross: ${(target any).zig}\" crossPkgs;\n\n#! Flake app helper (Without zig-env and root dir restriction).\napp-bare-no-root = deps: script: {\n type = \"app\";\n program = toString (pkgs.writeShellApplication {\n  name = \"app\";\n  runtimeInputs = [] ++ deps;\n  text = ''\n   # shellcheck disable=SC2059\n   error() { printf -- \"error: $1\\n\" \"''${@:2}\" 1\u003e\u00262; exit 1; }\n   ${script}\n  '';\n }) + \"/bin/app\";\n};\n\n#! Flake app helper (Without zig-env).\napp-bare = deps: script: app-bare-no-root deps ''\n [[ -f ./flake.nix ]] || error 'Run this from the project root'\n ${script}\n'';\n\n#! Flake app helper (without root dir restriction).\napp-no-root = deps: script: app-bare-no-root (deps ++ _deps) ''\n ${shell-runtime deps}\n ${script}\n'';\n\n#! Flake app helper.\napp = deps: script: app-bare (deps ++ _deps) ''\n ${shell-runtime deps}\n ${script}\n'';\n\n#! Creates dev shell.\nmkShell = pkgs.callPackage ({\n nativeBuildInputs ? [],\n ...\n } @attrs: pkgs.mkShellNoCC (attrs // {\n nativeBuildInputs = (remove zig.hook nativeBuildInputs) ++ _deps;\n shellHook = ''\n  ${shell-runtime nativeBuildInputs}\n  ${attrs.shellHook or \"\"}\n '';\n}));\n\n#! Packages zig project.\n#! NOTE: If your project has build.zig.zon you must first generate build.zig.zon2json-lock using zon2json-lock.\n#!       It is recommended to commit the build.zig.zon2json-lock to your repo.\n#!\n#! Additional attributes:\n#!    zigTarget: Specify target for zig compiler, defaults to stdenv.targetPlatform of given target.\n#!    zigPreferMusl: Prefer musl libc without specifying the target.\n#!    zigWrapperBins: Binaries available to the binary during runtime (PATH)\n#!    zigWrapperLibs: Libraries available to the binary during runtime (LD_LIBRARY_PATH)\n#!    zigWrapperArgs: Additional arguments to makeWrapper.\n#!    zigBuildZon: Path to build.zig.zon file, defaults to build.zig.zon.\n#!    zigBuildZonLock: Path to build.zig.zon2json-lock file, defaults to build.zig.zon2json-lock.\n#!\n#! \u003chttps://github.com/NixOS/nixpkgs/blob/master/doc/hooks/zig.section.md\u003e\npackage = zigPackage;\n\n#! Bundle a package into a zip\nbundle.zip = pkgs.callPackage ./src/bundle/zip.nix { inherit zigPackage; };\n\n\n#! Bundle a package for running in AWS lambda\nbundle.aws.lambda = pkgs.callPackage ./src/bundle/lambda.nix { bundleZip = bundle.zip; };\n\n#! --- Architecture dependent flake outputs.\n#!     access: `zig2nix.outputs.thing.${system}`\n\n#! Helper functions for building and running Zig projects.\ninherit zig-env;\n\n#! Versioned Zig packages.\n#! nix build .#zig-master\n#! nix build .#zig-latest\n#! nix run .#zig-0_13_0\npackages = mapAttrs' (k: v: nameValuePair (\"zig-\" + k) v) zigv;\n\n#! Develop shell for building and running Zig projects.\n#! nix develop .#zig_version\n#! example: nix develop .#master\n#! example: nix develop .#default\ndevShells = flake-outputs.devShells // {\n default = flake-outputs.devShells.latest;\n};\n\n#! --- Generic flake outputs.\n#!     access: `zig2nix.outputs.thing`\n\n#! Default project template\n#! nix flake init -t templates\ntemplates.default = rec {\n path = ./templates/default;\n description = \"Default Zig project template\";\n welcomeText = welcome-template description;\n};\n\n#! Master project template\n#! nix flake init -t templates#master\ntemplates.master = rec {\n path = ./templates/master;\n description = \"Master Zig project template\";\n welcomeText = welcome-template description;\n};\n```\n","funding_links":["https://github.com/sponsors/Cloudef"],"categories":["Programming Languages","Zig","Fundamentals"],"sub_categories":["Zig","Package and Version Manager"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCloudef%2Fzig2nix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCloudef%2Fzig2nix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCloudef%2Fzig2nix/lists"}