diff options
| -rw-r--r-- | .github/dependabot.yml | 8 | ||||
| -rw-r--r-- | .github/workflows/ci.yaml | 46 | ||||
| -rw-r--r-- | .github/workflows/update-flake.yaml | 27 | ||||
| -rw-r--r-- | .gitignore | 15 | ||||
| -rw-r--r-- | LICENSE | 19 | ||||
| -rw-r--r-- | README.md | 13 | ||||
| -rw-r--r-- | build.zig | 94 | ||||
| -rw-r--r-- | build.zig.zon | 36 | ||||
| -rw-r--r-- | deps.nix | 13 | ||||
| -rw-r--r-- | flake.lock | 99 | ||||
| -rw-r--r-- | flake.nix | 78 | ||||
| -rw-r--r-- | src/main.zig | 11 | ||||
| -rw-r--r-- | src/root.zig | 10 |
13 files changed, 469 insertions, 0 deletions
diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..4c39a33 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + commit-message: + prefix: "ci" diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..7a5bae0 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,46 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +jobs: + build: + name: Build Package + + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v11 + + - name: Setup Magic Nix Cache + uses: DeterminateSystems/magic-nix-cache-action@v6 + + - name: Run build + run: | + nix build --print-build-logs --show-trace + + release-gate: + name: Release gate + + if: ${{ always() }} + + needs: build + + runs-on: ubuntu-latest + + steps: + - name: Exit with error + if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') + run: exit 1 diff --git a/.github/workflows/update-flake.yaml b/.github/workflows/update-flake.yaml new file mode 100644 index 0000000..8430a2c --- /dev/null +++ b/.github/workflows/update-flake.yaml @@ -0,0 +1,27 @@ +name: Update flake.lock + +on: + schedule: + # run every saturday + - cron: "0 0 * * 6" + workflow_dispatch: + +jobs: + update: + name: Run update + + runs-on: ubuntu-latest + + permissions: + contents: write + pull-requests: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v11 + + - name: Update lockfile & make PR + uses: DeterminateSystems/update-flake-lock@v21 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e91702 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# zig +zig-cache/ +zig-out/ +build/ +build-*/ +docgen_tmp/ + +# direnv +.direnv +.envrc + +# nix +result +result-* +repl-result-* @@ -0,0 +1,19 @@ +Copyright (c) 2024 seth + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..33858c5 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# ziggy-with-it + +A small example of a [Nix flake](https://nix.dev/concepts/flakes) for a [Zig](https://ziglang.org/) project! + +## Features + +- A [development shell](https://nix.dev/tutorials/first-steps/ad-hoc-shell-environments) +- `zig` from `master` via [mitchellh/zig-overlay](https://github.com/mitchellh/zig-overlay) +- A package built with [`zig.hook`](https://ryantm.github.io/nixpkgs/hooks/zig/#zig-hook) and [`nix-community/zon2nix`](https://github.com/nix-community/zon2nix) + +## Thanks + +- @paperdave for teaching me how to use `zon` (...or just enough to make this flake work) diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..4316aaa --- /dev/null +++ b/build.zig @@ -0,0 +1,94 @@ +const std = @import("std"); + +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. +pub fn build(b: *std.Build) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); + + const lib = b.addStaticLibrary(.{ + .name = "ziggy-with-it", + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = .{ .path = "src/root.zig" }, + .target = target, + .optimize = optimize, + }); + + // This declares intent for the library to be installed into the standard + // location when the user invokes the "install" step (the default step when + // running `zig build`). + b.installArtifact(lib); + + const exe = b.addExecutable(.{ + .name = "ziggy-with-it", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // By making the run step depend on the install step, it will be run from the + // installation directory rather than directly from within the cache directory. + // This is not necessary, however, if the application depends on other installed + // files, this ensures they will be present and in the expected location. + run_cmd.step.dependOn(b.getInstallStep()); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const lib_unit_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/root.zig" }, + .target = target, + .optimize = optimize, + }); + + const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + + const exe_unit_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + + // Similar to creating the run step earlier, this exposes a `test` step to + // the `zig build --help` menu, providing a way for the user to request + // running the unit tests. + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_lib_unit_tests.step); + test_step.dependOn(&run_exe_unit_tests.step); + + const dep = b.dependency("known-folders", .{}); + exe.root_module.addImport("known-folders", dep.module("known-folders")); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..19b2806 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,36 @@ +.{ + .name = "ziggy-with-it", + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + //.minimum_zig_version = "0.11.0", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + .@"known-folders" = .{ + .url = "https://github.com/ziglibs/known-folders/archive/bf79988adcfce166f848e4b11e718c1966365329.tar.gz", + .hash = "12201314cffeb40c5e4e3da166217d2c74628c74486414aaf97422bcd2279915b9fd", + }, + }, + .paths = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/deps.nix b/deps.nix new file mode 100644 index 0000000..d205853 --- /dev/null +++ b/deps.nix @@ -0,0 +1,13 @@ +# generated by zon2nix (https://github.com/nix-community/zon2nix) + +{ linkFarm, fetchzip }: + +linkFarm "zig-packages" [ + { + name = "12201314cffeb40c5e4e3da166217d2c74628c74486414aaf97422bcd2279915b9fd"; + path = fetchzip { + url = "https://github.com/ziglibs/known-folders/archive/bf79988adcfce166f848e4b11e718c1966365329.tar.gz"; + hash = "sha256-Q7eMdyScqj8qEiAHg1BnGRTsWSQOKWWTc6hUYHNlgGg="; + }; + } +] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..61fb74d --- /dev/null +++ b/flake.lock @@ -0,0 +1,99 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1716941088, + "narHash": "sha256-GKSAGfLNocNTux33YT9GbEXwEewxepwFL+ViX1CrMCQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6c25325ec30a566f5c0446ceee61ada081903872", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "zig-overlay": "zig-overlay" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "zig-overlay": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1717028662, + "narHash": "sha256-tQGZgJzNPF4UPm+faSvEsBPwpLS9VpbyCeVlVOBtyKs=", + "owner": "mitchellh", + "repo": "zig-overlay", + "rev": "b908f8b849f5c58a4b70d09719d6d12a5c474da1", + "type": "github" + }, + "original": { + "owner": "mitchellh", + "repo": "zig-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..0733e33 --- /dev/null +++ b/flake.nix @@ -0,0 +1,78 @@ +{ + description = "oh yeah. we're getting ziggy with it now."; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + zig-overlay = { + url = "github:mitchellh/zig-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { + self, + nixpkgs, + zig-overlay, + ... + }: let + systems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + + forAllSystems = fn: nixpkgs.lib.genAttrs systems (system: fn nixpkgs.legacyPackages.${system}); + + # https://github.com/mitchellh/zig-overlay?tab=readme-ov-file#usage + zigVersion = "master-2024-05-08"; + zigFor = system: zig-overlay.packages.${system}.${zigVersion}; + in { + devShells = forAllSystems ({ + pkgs, + system, + ... + }: { + default = pkgs.mkShellNoCC { + inputsFrom = [self.packages.${system}.ziggy-with-it]; + }; + }); + + packages = forAllSystems ({ + lib, + pkgs, + system, + ... + }: rec { + default = ziggy-with-it; + ziggy-with-it = pkgs.stdenvNoCC.mkDerivation { + pname = "ziggy-with-it"; + version = self.shortRev or self.dirtyShortRev or "waaaa"; + + src = with lib.fileset; + toSource { + root = ./.; + fileset = unions [ + (gitTracked ./src) + ./build.zig + ./build.zig.zon + ]; + }; + + # `deps.nix` is generated with by running `zon2nix` + # https://github.com/nix-community/zon2nix + postPatch = '' + ln -s ${pkgs.callPackage ./deps.nix {}} $ZIG_GLOBAL_CACHE_DIR/p + ''; + + nativeBuildInputs = [ + (pkgs.zig.hook.override { + # FIXME: `zig.hook` requires `zig` to have it's `meta` attribute + # zig-overlay requires this `meta` attribute..yay + zig = zigFor system // {inherit (pkgs.zig) meta;}; + }) + ]; + }; + }); + }; +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..75e0b4b --- /dev/null +++ b/src/main.zig @@ -0,0 +1,11 @@ +const std = @import("std"); +const known = @import("known-folders"); + +pub fn main() !void { + const j = try known.getPath(std.heap.page_allocator, .home) orelse { + std.debug.print("Womp womp.\n", .{}); + return error.CouldNotFindHome; + }; + + std.debug.print("~ is where the heart is -- or {s}.\n", .{j}); +} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 0000000..ecfeade --- /dev/null +++ b/src/root.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const testing = std.testing; + +export fn add(a: i32, b: i32) i32 { + return a + b; +} + +test "basic add functionality" { + try testing.expect(add(3, 7) == 10); +} |
