]> gitweb.ps.run Git - chirp/commitdiff
update flake.nix
authorpatrick-scho <patrick.schoenberger@posteo.de>
Fri, 11 Apr 2025 11:12:24 +0000 (13:12 +0200)
committerpatrick-scho <patrick.schoenberger@posteo.de>
Fri, 11 Apr 2025 11:12:24 +0000 (13:12 +0200)
flake.nix

index fd691a8755163ebfec058e5c21762ba335dc08bb..60987e717a0bfc63b57b7e2daf88d1110d1573f8 100644 (file)
--- a/flake.nix
+++ b/flake.nix
@@ -1,56 +1,95 @@
 {
-  description = "Chirp!";
+  description = "Zig project flake";
 
   inputs = {
-    zig.url = "github:mitchellh/zig-overlay";
+    zig2nix.url = "github:Cloudef/zig2nix";
   };
 
-  outputs = {
-    self,
-    zig,
-    pkgs,
-  }: let
-    nixosModule = {
-      config,
-      lib,
-      pkgs,
-      ...
-    }: {
-      options.services.chirp = {
-        enable = lib.mkEnableOption "Chirp";
-
-        port = lib.mkOption {
-          type = lib.types.port;
-          default = 8080;
-          description = "Port to listen on";
-        };
+  outputs = { zig2nix, ... }: let
+    flake-utils = zig2nix.inputs.flake-utils;
+  in (flake-utils.lib.eachDefaultSystem (system: let
+      # Zig flake helper
+      # Check the flake.nix in zig2nix project for more options:
+      # <https://github.com/Cloudef/zig2nix/blob/master/flake.nix>
+      env = zig2nix.outputs.zig-env.${system} {};
+      zighttp = env.pkgs.fetchFromGitHub {
+        owner = "patrick-scho";
+        repo = "zighttp";
+        rev = "274c46eb3f7e9987cc9260bb70c43b4f5d8555ac";
+        sha256 = "sha256-SUFHiA0r46QfzhlO6Ie1CByzC/+qPR2kyAbeDEdKOJ0=";
       };
+      ziglmdb = env.pkgs.fetchFromGitHub {
+        owner = "patrick-scho";
+        repo = "ziglmdb";
+        rev = "88cb74a430fa9629c8127c3a866c40e79a8e4612";
+        sha256 = "sha256-SUFHiA0r46QfzhlO6Ie1CByzC/+qPR2kyAbeDEdKOJ0=";
+      };
+    in with builtins; with env.pkgs.lib; rec {
+      # Produces clean binaries meant to be ship'd outside of nix
+      # nix build .#foreign
+      packages.foreign = env.package {
+        src = cleanSource ./.;
+
+        # Packages required for compiling
+        nativeBuildInputs = with env.pkgs; [];
 
-      config = lib.mkIf config.services.chirp.enable {
-        systemd.services.chirp = {
-          description = "Chirp SystemD Service!";
-          wantedBy = ["multi-user.target"];
-          after = ["network.target"];
-          serviceConfig = {
-            ExecStart = "${zig.packages.master}";
-            Restart = "always";
-            Type = "simple";
-            DynamicUser = "yes";
-          };
-          environment = {
-            PORT = toString config.services.chirp.port;
-          };
-        };
+        # Packages required for linking
+        buildInputs = with env.pkgs; [];
+
+        # Smaller binaries and avoids shipping glibc.
+        zigPreferMusl = true;
       };
-    };
-    in {
-      # TODO: packages.default build
-      apps.default = {
+
+      # nix build .
+      packages.default = packages.foreign.override (attrs: {
+        # Prefer nix friendly settings.
+        zigPreferMusl = false;
+
+        # Executables required for runtime
+        # These packages will be added to the PATH
+        zigWrapperBins = with env.pkgs; [];
+
+        # Libraries required for runtime
+        # These packages will be added to the LD_LIBRARY_PATH
+        zigWrapperLibs = attrs.buildInputs or [];
+      });
+
+      # For bundling with nix bundle for running outside of nix
+      # example: https://github.com/ralismark/nix-appimage
+      apps.bundle = {
         type = "app";
-        program = "${zig.packages.${pkgs.system}."0.14.0"} build run";
+        program = "${packages.foreign}/bin/default";
+      };
+
+      # nix run .
+      apps.default = env.app [] "zig build run -- \"$@\"";
+
+      # nix run .#build
+      apps.build = env.app [] ''
+          mkdir -p ext
+          ln -s ${zighttp} ext/zighttp
+          ln -s ${ziglmdb} ext/ziglmdb
+          zig build "$@"
+      '';
+
+      # nix run .#test
+      apps.test = env.app [] "zig build test -- \"$@\"";
+
+      # nix run .#docs
+      apps.docs = env.app [] "zig build docs -- \"$@\"";
+
+      # nix run .#zig2nix
+      apps.zig2nix = env.app [] "zig2nix \"$@\"";
+
+      # nix develop
+      devShells.default = env.mkShell {
+        # Packages required for compiling, linking and running
+        # Libraries added here will be automatically added to the LD_LIBRARY_PATH and PKG_CONFIG_PATH
+        nativeBuildInputs = []
+          ++ packages.default.nativeBuildInputs
+          ++ packages.default.buildInputs
+          ++ packages.default.zigWrapperBins
+          ++ packages.default.zigWrapperLibs;
       };
-    }
-    // {
-      nixosModules.default = nixosModule;
-    };
+    }));
 }