Skip to content

Commit

Permalink
Add zig template (#49)
Browse files Browse the repository at this point in the history
* feat: add zig template

* feat: README,workflows,add template to flake.nix

* feat: re add the dart github workflow

* feat: gitignore

* .envrc

* .envrc
  • Loading branch information
SecretVal authored Jan 31, 2024
1 parent c6cb4da commit 85aa00e
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/flake-language.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- python-pkg
- rust
- dart
- zig
steps:
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
Expand All @@ -44,6 +45,7 @@ jobs:
- python-app
- rust
- dart
- zig
steps:
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
Expand Down
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,17 @@ doc/api/
.env

# End of https://www.toptal.com/developers/gitignore/api/dart

# Created by https://www.toptal.com/developers/gitignore/api/zig
# Edit at https://www.toptal.com/developers/gitignore?templates=zig

### zig ###
# Zig programming language

zig-cache/
zig-out/
build/
build-*/
docgen_tmp/

# End of https://www.toptal.com/developers/gitignore/api/zig
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Kickstart your Nix environments.
- [Python (package)](#python-package)
- [Rust](#rust)
- [Dart](#dart)
- [Zig](#zig)
- Systems
- Linux
- [Home Manager](#home-manager)
Expand Down Expand Up @@ -392,6 +393,14 @@ Used for Dart applications.
nix flake init -t github:ALT-F4-LLC/kickstart.nix#dart
```

#### Zig

Used for Zig applications.

```bash
nix flake init -t github:ALT-F4-LLC/kickstart.nix#zig
```

### Systems

#### Home Manager
Expand Down
5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
description = "Kickstart Dart package flake.";
path = ./template/dart;
};
zig = {
description = "Kickstart Zig package flake.";
path = ./template/zig;
};
};
};

Expand Down Expand Up @@ -111,6 +115,7 @@
example-python-pkg = lib.flake.python-pkg system;
example-rust = lib.flake.rust system;
example-dart = lib.flake.dart system;
example-zig = lib.flake.zig system;
};
};

Expand Down
13 changes: 13 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,18 @@
cp --no-preserve=mode -r $src/* $out
cat $out/flake.nix
'';

zig = system:
let
pkgs = inputs.nixpkgs.legacyPackages.${system};
in
pkgs.runCommand "zig"
{
src = ../template/zig;
} ''
mkdir -p $out
cp --no-preserve=mode -r $src/* $out
cat $out/flake.nix
'';
};
}
1 change: 1 addition & 0 deletions template/zig/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
70 changes: 70 additions & 0 deletions template/zig/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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 exe = b.addExecutable(.{
.name = "example",
// 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/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 unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

const run_unit_tests = b.addRunArtifact(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_unit_tests.step);
}
39 changes: 39 additions & 0 deletions template/zig/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
description = "Example kickstart Zig application project.";

inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
perSystem = { config, self', inputs', pkgs, system, ... }:
let
inherit (pkgs) dockerTools stdenv zig_0_11;
inherit (dockerTools) buildImage;
name = "example";
version = "0.1.0";
in
{
packages = {
default = stdenv.mkDerivation {
inherit name;
inherit version;
src = ./.;
nativeBuildInputs = [
zig_0_11.hook
];
};
docker = buildImage {
inherit name;
tag = version;
config = {
Cmd = "${self'.packages.default}/bin/${name}";
Env = [
"SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
];
};
};
};
};
};
}
24 changes: 24 additions & 0 deletions template/zig/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const std = @import("std");

pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)main
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();

try stdout.print("Run `zig build test` to run the tests.\n", .{});

try bw.flush(); // don't forget to flush!
}

test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}

0 comments on commit 85aa00e

Please sign in to comment.