From 2279e958bf88a613fc438372d1061157d858b8e3 Mon Sep 17 00:00:00 2001 From: xeals Date: Thu, 30 Nov 2023 12:12:51 +1100 Subject: [PATCH] Initial commit Include Nix package, build pipeline --- .gitea/workflows/build.yml | 16 +++++++++ .gitignore | 71 ++++++++++++++++++++++++++++++++++++++ README.md | 3 ++ build.zig | 70 +++++++++++++++++++++++++++++++++++++ flake.lock | 61 ++++++++++++++++++++++++++++++++ flake.nix | 61 ++++++++++++++++++++++++++++++++ src/main.zig | 24 +++++++++++++ 7 files changed, 306 insertions(+) create mode 100644 .gitea/workflows/build.yml create mode 100644 .gitignore create mode 100644 README.md create mode 100644 build.zig create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/main.zig diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..f10ce93 --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,16 @@ +name: "Build and test" +on: + push: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: https://gitea.com/actions/checkout@v3 + - name: Install nix + uses: https://github.com/cachix/install-nix-action@v23 + with: + github_access_token: ${{ secrets.INPUT_GITHUB_ACCESS_TOKEN }} + - name: Build package + run: nix build diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9876873 --- /dev/null +++ b/.gitignore @@ -0,0 +1,71 @@ +# Created by https://www.toptal.com/developers/gitignore/api/direnv,emacs,zig +# Edit at https://www.toptal.com/developers/gitignore?templates=direnv,emacs,zig + +### direnv ### +.direnv +.envrc + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data + + +### zig ### +# Zig programming language + +zig-cache/ +zig-out/ +build/ +build-*/ +docgen_tmp/ + +# End of https://www.toptal.com/developers/gitignore/api/direnv,emacs,zig + +### nix ### +result diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea355cc --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# AOC23 + +Advent of Code 2023 diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..c7c73a4 --- /dev/null +++ b/build.zig @@ -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 = "aoc23", + // 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); +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..c875998 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1701174899, + "narHash": "sha256-1W+FMe8mWsJKXoBc+QgKmEeRj33kTFnPq7XCjU+bfnA=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "010c7296f3b19a58b206fdf7d68d75a5b0a09e9e", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4d7923d --- /dev/null +++ b/flake.nix @@ -0,0 +1,61 @@ +{ + description = "Advent of Code 2023"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + inherit (nixpkgs) lib; + pkgs = import nixpkgs { inherit system; }; + zig = pkgs.zig_0_11; + in + { + devShells = { + default = pkgs.mkShellNoCC { + buildInputs = [ zig pkgs.zls ]; + }; + ci = pkgs.mkShellNoCC { + buildInputs = [ zig ]; + }; + }; + + packages.default = + let + optimize = "ReleaseSafe"; + in + pkgs.stdenv.mkDerivation { + name = "aoc23"; + src = lib.cleanSource ./.; + + buildInputs = [ zig ]; + + # https://github.com/ziglang/zig/issues/6810 + XDG_CACHE_HOME = "xdg_cache"; + + zigFlags = [ "-Doptimize=${optimize}" ]; + + buildPhase = '' + runHook preBuild + zig build $zigFlags + runHook postBuild + ''; + + doCheck = true; + checkPhase = '' + runHook preCheck + zig build test $zigFlags + runHook postCheck + ''; + + installPhase = '' + runHook preInstall + zig build install --prefix $out $zigFlags + runHook postInstall + ''; + }; + }); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..c8a3f67 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,24 @@ +const std = @import("std"); + +pub fn main() !void { + // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) + 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()); +}