From a50240ec576634b66e7b187edcbd78179e52aaec Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 10 Jul 2026 23:00:28 -0400 Subject: [PATCH] Add support for building natter via nix. --- .gitignore | 1 + Cargo.toml | 2 +- flake.lock | 48 +++++++++++++++++++++++++++ flake.nix | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ nix/package.nix | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/package.nix diff --git a/.gitignore b/.gitignore index ea8c4bf..d787b70 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/result diff --git a/Cargo.toml b/Cargo.toml index 8ed8363..3802cc7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ default = ["tracing"] tracing = ["dep:opentelemetry", "dep:opentelemetry-otlp", "dep:opentelemetry-semantic-conventions", "dep:tracing", "dep:tracing-opentelemetry", "dep:tracing-subscriber"] # Optimized build for any sort of release. -[profile.release-lto] +[profile.lto] inherits = "release" lto = true strip = "symbols" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..85aa85c --- /dev/null +++ b/flake.lock @@ -0,0 +1,48 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1780749050, + "narHash": "sha256-3av0pIjlOWQ6rDbNOmpUSvbNnJkGORQKKjb4LtCZsIY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a799d3e3886da994fa307f817a6bc705ae538eeb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1781407245, + "narHash": "sha256-VzJq4MmD0uyNDAceudSe1hHqcQMe9Tau0U4S+5iRGh0=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "d5f483210eb016d66102eef22baa128b3b3233fc", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..aa0b16c --- /dev/null +++ b/flake.nix @@ -0,0 +1,86 @@ +{ + description = "Natter static site generator"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + }; + }; + }; + + outputs = + { + self, + nixpkgs, + rust-overlay, + }: + let + forAllSystems = + func: + builtins.listToAttrs ( + map (system: { + name = system; + value = func system; + }) nixpkgs.lib.systems.flakeExposed + ); + in + { + devShells = forAllSystems ( + system: + let + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + rustToolchain = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml); + in + { + default = pkgs.mkShell { + nativeBuildInputs = [ + rustToolchain + ]; + buildInputs = with pkgs; [ + ]; + }; + } + ); + packages = forAllSystems ( + system: + let + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + appliedOverlay = self.overlays.default pkgs pkgs; + in + rec { + default = release; + inherit (appliedOverlay.natter) + release + ; + docker_env = pkgs.buildEnv { + name = "natter"; + paths = with pkgs; [ + appliedOverlay.natter.release + bash + uutils-coreutils-noprefix + # toybox # Smaller than uutils-coreutils? + ]; + }; + } + ); + overlays.default = final: prev: { + natter = final.lib.makeScope final.newScope (natterScope: { + release = ( + natterScope.callPackage ./nix/package.nix { + inherit rust-overlay; + buildType = "lto"; + } + ); + }); + }; + }; +} diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..569ef37 --- /dev/null +++ b/nix/package.nix @@ -0,0 +1,82 @@ +{ + hello, + lib, + pkgs, + rust-overlay, + + targetBins ? [ ], + features ? [ ], + cargoBuildTarget ? null, + buildType ? null, + buildLib ? false, +}: +let + cargoToml = (lib.importTOML ../Cargo.toml); + rustPlatformFor = + pkgs: + let + rust-bin = rust-overlay.lib.mkRustBin { } pkgs; + rustToolchain = (rust-bin.fromRustupToolchainFile ../rust-toolchain.toml).override ( + if cargoBuildTarget != null then + { + targets = [ cargoBuildTarget ]; + } + else + { } + ); + baseRustPlatform = ( + pkgs.makeRustPlatform { + cargo = rustToolchain; + rustc = rustToolchain; + } + ); + rustPlatform = + if cargoBuildTarget != null then + baseRustPlatform.overrideScope ( + final: prev: { + cargoBuildHook = prev.cargoBuildHook.overrideDerivation (_: { + rustcTargetSpec = cargoBuildTarget; + }); + cargoInstallHook = hello; + } + ) + else + baseRustPlatform; + in + rustPlatform; +in +(rustPlatformFor pkgs).buildRustPackage ( + # rustPlatform.buildRustPackage ( + { + pname = "natter"; + version = cargoToml.package.version; + src = lib.cleanSource ../.; + + cargoLock.lockFile = ../Cargo.lock; + + cargoBuildFlags = builtins.concatLists [ + (builtins.concatMap (targetBin: [ + "--bin" + targetBin + ]) targetBins) + (if buildLib then [ "--lib" ] else [ ]) + ( + if features != [ ] then + [ + "--features" + (builtins.concatStringsSep " " features) + ] + else + [ ] + ) + ]; + } + // ( + if buildType != null then + { + buildType = buildType; + } + else + { } + ) +)