about summary refs log tree commit diff
path: root/flake.nix
diff options
context:
space:
mode:
authorvenomade <venomade@venomade.com>2026-05-21 20:34:45 +0100
committervenomade <venomade@venomade.com>2026-05-21 20:34:45 +0100
commit637409d951e9dd1a2c29cd424bd41ff8c14b6d88 (patch)
tree2d41be117f6a9f62562c7b54f06a1b1780c62a3b /flake.nix
Initial Commit main
Diffstat (limited to 'flake.nix')
-rw-r--r--flake.nix169
1 files changed, 169 insertions, 0 deletions
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..25444f8
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,169 @@
+{
+  description = "sild - The Small Interactive Lua Debugger";
+
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
+
+    # Fenix provides the rust toolchains
+    fenix.url = "github:nix-community/fenix";
+    fenix.inputs.nixpkgs.follows = "nixpkgs";
+
+    # Naersk builds the rust project
+    naersk.url = "github:nix-community/naersk";
+    naersk.inputs.nixpkgs.follows = "nixpkgs";
+
+    flake-utils.url = "github:numtide/flake-utils";
+  };
+
+  outputs =
+    {
+      self,
+      nixpkgs,
+      fenix,
+      naersk,
+      flake-utils,
+    }:
+    let
+      # TODO: Add x86_64-darwin and aarch64-darwin
+      buildTargets = {
+        "x86_64-linux" = {
+          crossSystemConfig = "x86_64-unknown-linux-musl";
+          rustTarget = "x86_64-unknown-linux-musl";
+        };
+        "aarch64-linux" = {
+          crossSystemConfig = "aarch64-unknown-linux-musl";
+          rustTarget = "aarch64-unknown-linux-musl";
+        };
+      };
+
+      makePackage =
+        buildSystem: targetName:
+        let
+          target = buildTargets.${targetName};
+          rustTarget = target.rustTarget;
+
+          pkgs = import nixpkgs {
+            system = buildSystem;
+            overlays = [ fenix.overlays.default ];
+          };
+
+          pkgsCross = import nixpkgs {
+            system = buildSystem;
+            crossSystem = {
+              config = target.crossSystemConfig;
+              isStatic = true;
+            };
+            overlays = [ fenix.overlays.default ];
+          };
+
+          fenixPkgs = pkgs.fenix;
+
+          toolchain = fenixPkgs.combine [
+            fenixPkgs.stable.rustc
+            fenixPkgs.stable.cargo
+            fenixPkgs.targets.${rustTarget}.stable.rust-std
+          ];
+
+          naersk-lib = pkgs.callPackage naersk {
+            cargo = toolchain;
+            rustc = toolchain;
+          };
+
+          targetCC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc";
+
+        in
+        naersk-lib.buildPackage {
+          name = "sild-${targetName}";
+          src = ./.;
+
+          strictDeps = true;
+          doCheck = false;
+
+          # Don't strip Lua
+          singleStep = true;
+
+          TARGET_CC = targetCC;
+          "CC_${builtins.replaceStrings [ "-" ] [ "_" ] rustTarget}" = targetCC;
+
+          CARGO_BUILD_TARGET = rustTarget;
+          CARGO_BUILD_RUSTFLAGS = [
+            "-C"
+            "target-feature=+crt-static"
+            "-C"
+            "link-args=-static"
+            "-C"
+            "linker=${targetCC}"
+          ];
+        };
+
+      perSystemOutputs =
+        buildSystem:
+        let
+          pkgs = import nixpkgs {
+            system = buildSystem;
+            overlays = [ fenix.overlays.default ];
+          };
+
+          devToolchain = pkgs.fenix.stable.withComponents [
+            "rustc"
+            "cargo"
+            "rust-src"
+            "clippy"
+            "rustfmt"
+            "rust-std"
+          ];
+
+        in
+        {
+          packages =
+            builtins.mapAttrs (targetName: _: makePackage buildSystem targetName) buildTargets
+            //
+              (
+                let
+                  nativeTarget =
+                    if pkgs.stdenv.isLinux then
+                      "${builtins.head (builtins.match "([^-]+)-.*" buildSystem)}-linux"
+                    else
+                      null;
+                in
+                if nativeTarget != null && buildTargets ? ${nativeTarget} then
+                  { default = makePackage buildSystem nativeTarget; }
+                else
+                  { }
+              );
+
+          devShells.default = pkgs.mkShell {
+            name = "sild-dev";
+
+            nativeBuildInputs = [
+              devToolchain
+              pkgs.rust-analyzer
+              pkgs.pkg-config
+
+              # Cross linkers for musl
+              pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc
+              pkgs.pkgsCross.musl64.stdenv.cc
+            ];
+
+            # Musl cross linkers locations
+            CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${pkgs.pkgsCross.musl64.stdenv.cc}/bin/${pkgs.pkgsCross.musl64.stdenv.cc.targetPrefix}cc";
+            CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER = "${pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc}/bin/${pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc.targetPrefix}cc";
+
+            shellHook = ''
+              echo ""
+              echo "  SILD Nix Devenv"
+              echo "  Rust: v$(rustc --version |  awk '{print $2}')"
+              echo "  mlua: $(cargo tree | grep 'mlua v' | awk '{print $3}')"
+              echo ""
+              echo "  Build commands:"
+              echo "    cargo build --release                                     # native debug build"
+              echo "    nix build .#x86_64-linux                                  # static x86_64 build"
+              echo "    nix build .#aarch64-linux                                 # static aarch64 build"
+              echo ""
+            '';
+          };
+        };
+
+    in
+    flake-utils.lib.eachDefaultSystem (system: perSystemOutputs system);
+}