From 0a4a12553756ad391cfefae7c7fac31b09e66efc Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Fri, 10 Oct 2025 07:11:52 -0500 Subject: [PATCH 1/8] First iteration of template --- flake.nix | 6 +++ rust/.envrc | 1 + rust/.gitignore | 5 +++ rust/Cargo.lock | 7 +++ rust/Cargo.toml | 4 ++ rust/flake.nix | 115 +++++++++++++++++++++++++++++++++++++++++++++++ rust/src/main.rs | 3 ++ 7 files changed, 141 insertions(+) create mode 100644 rust/.envrc create mode 100644 rust/.gitignore create mode 100644 rust/Cargo.lock create mode 100644 rust/Cargo.toml create mode 100644 rust/flake.nix create mode 100644 rust/src/main.rs diff --git a/flake.nix b/flake.nix index 0f66bf1..8eb5ff6 100644 --- a/flake.nix +++ b/flake.nix @@ -57,6 +57,12 @@ https://search.nixos.org/options ''; }; + + rust = { + description = "TODO"; + path = ./rust; + welcomeText = "TODO"; + }; }; }; } diff --git a/rust/.envrc b/rust/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/rust/.envrc @@ -0,0 +1 @@ +use flake diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 0000000..6263dee --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1,5 @@ +# Rust artifacts +/target/ + +# Nix artifacts +result* \ No newline at end of file diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 0000000..ece5877 --- /dev/null +++ b/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rust-flake-template" +version = "0.1.0" diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..a1fd3f1 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "rust-flake-template" +version = "0.1.0" +edition = "2024" diff --git a/rust/flake.nix b/rust/flake.nix new file mode 100644 index 0000000..6fd9c6c --- /dev/null +++ b/rust/flake.nix @@ -0,0 +1,115 @@ +{ + description = "A Rust flake template that you can adapt to your own environment"; + + # Flake inputs + inputs = { + nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1"; # Unstable Nixpkgs + # Rust toolchain + fenix = { + url = "https://flakehub.com/f/nix-community/fenix/0.1"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + # Rust builder + crane.url = "https://flakehub.com/f/ipetkov/crane/0"; + }; + + # Flake outputs + outputs = + { self, ... }@inputs: + let + # The systems supported for this flake's outputs + supportedSystems = [ + "x86_64-linux" # 64-bit Intel/AMD Linux + "aarch64-linux" # 64-bit ARM Linux + "x86_64-darwin" # 64-bit Intel macOS + "aarch64-darwin" # 64-bit ARM macOS + ]; + + # Helper for providing system-specific attributes + forEachSupportedSystem = + f: + inputs.nixpkgs.lib.genAttrs supportedSystems ( + system: + f { + # Provides a system-specific, configured Nixpkgs + pkgs = import inputs.nixpkgs { + inherit system; + # Enable using unfree packages + config.allowUnfree = true; + overlays = [ self.overlays.default ]; + }; + } + ); + + lastModifiedDate = inputs.self.lastModifiedDate or inputs.self.lastModified or "19700101"; + version = "${builtins.substring 0 8 lastModifiedDate}-${inputs.self.shortRev or "dirty"}"; + meta = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package; + in + { + # Development environments output by this flake + devShells = forEachSupportedSystem ( + { pkgs }: + { + # Run `nix develop` to activate this environment or `direnv allow` if you have direnv installed + default = pkgs.mkShell { + # The Nix packages provided in the environment + packages = with pkgs; [ + rustToolchain + ]; + + # Set any environment variables for your development environment + env = { + RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library"; + }; + + # Add any shell logic you want executed when the environment is activated + shellHook = '' + echo "Rust toolchain 🦀" + cargo --version + ''; + }; + } + ); + + # Package outputs + packages = forEachSupportedSystem ( + { pkgs }: + { + default = pkgs.buildRustPackage ./.; + } + ); + + # An overlay that puts the Rust toolchain in `pkgs` + overlays.default = + final: prev: + let + rustToolchain = + with inputs.fenix.packages.${prev.system}; + combine ( + with stable; + [ + cargo + clippy + rustc + rustfmt + rust-src + rust-analyzer + ] + ); + in + { + inherit rustToolchain; + + buildRustPackage = + src: + ((inputs.crane.mkLib final).overrideToolchain rustToolchain).buildPackage ({ + pname = meta.name; + inherit (meta) version; + src = builtins.path { + name = "${meta.name}-source"; + path = src; + }; + }); + }; + }; +} diff --git a/rust/src/main.rs b/rust/src/main.rs new file mode 100644 index 0000000..957a7f6 --- /dev/null +++ b/rust/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello from the Rust template!"); +} From c9f2dca65ab4382c5178fc1bab1c351980eb0c79 Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Fri, 10 Oct 2025 07:16:14 -0500 Subject: [PATCH 2/8] Add Rust template checks --- .github/workflows/ci.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 73f087f..42a8e48 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -42,4 +42,15 @@ jobs: - name: Check NixOS template run: | + echo "Building NixOS toplevel ❄️" nix build --no-link ./nixos#nixosConfigurations.my-system.config.system.build.toplevel + echo "NixOS template OK ✅" + + - name: Check Rust template + run: | + system=$(nix eval --raw --impure --expr 'builtins.currentSystem') + echo "Build Rust dev shell 🦀" + nix build "./rust#devShells.${system}.default" + echo "Building Rust package 🦀" + nix build './rust' + echo "Rust template OK ✅" From 389a5dcfce87b2e330ff483be8169214c0b69a30 Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Fri, 10 Oct 2025 07:21:47 -0500 Subject: [PATCH 3/8] Move package builder function out of overlay --- rust/flake.nix | 57 +++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/rust/flake.nix b/rust/flake.nix index 6fd9c6c..1489bac 100644 --- a/rust/flake.nix +++ b/rust/flake.nix @@ -75,41 +75,32 @@ packages = forEachSupportedSystem ( { pkgs }: { - default = pkgs.buildRustPackage ./.; + default = ((inputs.crane.mkLib pkgs).overrideToolchain pkgs.rustToolchain).buildPackage { + pname = meta.name; + inherit (meta) version; + src = builtins.path { + name = "${meta.name}-source"; + path = ./.; + }; + }; } ); - # An overlay that puts the Rust toolchain in `pkgs` - overlays.default = - final: prev: - let - rustToolchain = - with inputs.fenix.packages.${prev.system}; - combine ( - with stable; - [ - cargo - clippy - rustc - rustfmt - rust-src - rust-analyzer - ] - ); - in - { - inherit rustToolchain; - - buildRustPackage = - src: - ((inputs.crane.mkLib final).overrideToolchain rustToolchain).buildPackage ({ - pname = meta.name; - inherit (meta) version; - src = builtins.path { - name = "${meta.name}-source"; - path = src; - }; - }); - }; + # An overlay that puts the Rust toolchain `pkgs` + overlays.default = final: prev: { + rustToolchain = + with inputs.fenix.packages.${prev.system}; + combine ( + with stable; + [ + cargo + clippy + rustc + rustfmt + rust-src + rust-analyzer + ] + ); + }; }; } From f4006ed7ecc2d6bde60520d4b7c93f0ae4b37c1e Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Mon, 13 Oct 2025 21:09:45 -0500 Subject: [PATCH 4/8] Add cargo-* tools and overlay for crane --- rust/flake.nix | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/rust/flake.nix b/rust/flake.nix index 1489bac..e1c12e8 100644 --- a/rust/flake.nix +++ b/rust/flake.nix @@ -3,10 +3,10 @@ # Flake inputs inputs = { - nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1"; # Unstable Nixpkgs + nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs # Rust toolchain fenix = { - url = "https://flakehub.com/f/nix-community/fenix/0.1"; + url = "https://flakehub.com/f/nix-community/fenix/0"; inputs.nixpkgs.follows = "nixpkgs"; }; # Rust builder @@ -55,6 +55,15 @@ # The Nix packages provided in the environment packages = with pkgs; [ rustToolchain + rust-analyzer # Rust language server for IDEs + # Uncomment the lines below for some helpful tools: + # cargo-edit # Commands like `cargo add` and `cargo rm` + # bacon # For iterative development + # cargo-nextest # Rust testing tool + # cargo-audit # Check dependencies for vulnerabilities + # cargo-outdated # Show which dependencies have updates available + # cargo-deny # Lint your dependency graph + # cargo-expand # Show macro expansions ]; # Set any environment variables for your development environment @@ -75,7 +84,7 @@ packages = forEachSupportedSystem ( { pkgs }: { - default = ((inputs.crane.mkLib pkgs).overrideToolchain pkgs.rustToolchain).buildPackage { + default = pkgs.craneBuild.buildPackage { pname = meta.name; inherit (meta) version; src = builtins.path { @@ -87,7 +96,9 @@ ); # An overlay that puts the Rust toolchain `pkgs` - overlays.default = final: prev: { + overlays.default = final: prev: rec { + craneBuild = ((inputs.crane.mkLib final).overrideToolchain rustToolchain); + rustToolchain = with inputs.fenix.packages.${prev.system}; combine ( @@ -98,7 +109,6 @@ rustc rustfmt rust-src - rust-analyzer ] ); }; From f49921acb87575882bf9c94f3cfd8f1a7a6b224e Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Wed, 15 Oct 2025 00:47:08 -0300 Subject: [PATCH 5/8] Use only Nixpkgs machinery --- rust/flake.nix | 60 +++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/rust/flake.nix b/rust/flake.nix index e1c12e8..f791092 100644 --- a/rust/flake.nix +++ b/rust/flake.nix @@ -2,16 +2,7 @@ description = "A Rust flake template that you can adapt to your own environment"; # Flake inputs - inputs = { - nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs - # Rust toolchain - fenix = { - url = "https://flakehub.com/f/nix-community/fenix/0"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - # Rust builder - crane.url = "https://flakehub.com/f/ipetkov/crane/0"; - }; + inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs # Flake outputs outputs = @@ -36,14 +27,9 @@ inherit system; # Enable using unfree packages config.allowUnfree = true; - overlays = [ self.overlays.default ]; }; } ); - - lastModifiedDate = inputs.self.lastModifiedDate or inputs.self.lastModified or "19700101"; - version = "${builtins.substring 0 8 lastModifiedDate}-${inputs.self.shortRev or "dirty"}"; - meta = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package; in { # Development environments output by this flake @@ -54,7 +40,10 @@ default = pkgs.mkShell { # The Nix packages provided in the environment packages = with pkgs; [ - rustToolchain + cargo + rustc + clippy + rustfmt rust-analyzer # Rust language server for IDEs # Uncomment the lines below for some helpful tools: # cargo-edit # Commands like `cargo add` and `cargo rm` @@ -68,7 +57,7 @@ # Set any environment variables for your development environment env = { - RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library"; + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; }; # Add any shell logic you want executed when the environment is activated @@ -84,33 +73,20 @@ packages = forEachSupportedSystem ( { pkgs }: { - default = pkgs.craneBuild.buildPackage { - pname = meta.name; - inherit (meta) version; - src = builtins.path { - name = "${meta.name}-source"; - path = ./.; + # Build the package using Nixpkgs' built-in Rust helpers + default = + let + # Get information about the package from Cargo.toml + meta = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package; + in + pkgs.rustPlatform.buildRustPackage { + inherit (meta) name version; + src = builtins.path { + path = ./.; + }; + cargoLock.lockFile = ./Cargo.lock; }; - }; } ); - - # An overlay that puts the Rust toolchain `pkgs` - overlays.default = final: prev: rec { - craneBuild = ((inputs.crane.mkLib final).overrideToolchain rustToolchain); - - rustToolchain = - with inputs.fenix.packages.${prev.system}; - combine ( - with stable; - [ - cargo - clippy - rustc - rustfmt - rust-src - ] - ); - }; }; } From 9d87fbf996da311c0accdd6362e03c1d7c8bde50 Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Wed, 15 Oct 2025 00:49:37 -0300 Subject: [PATCH 6/8] Fill out the description and welcome text --- flake.nix | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/flake.nix b/flake.nix index 8eb5ff6..727f710 100644 --- a/flake.nix +++ b/flake.nix @@ -59,9 +59,21 @@ }; rust = { - description = "TODO"; + description = "A flake template for Rust"; path = ./rust; - welcomeText = "TODO"; + welcomeText = '' + # Welcome to your new NixOS configuration flake ❄️🦀 + + To activate your new flake's development environment, run `nix develop` or `direnv allow` if you use direnv. + + To run the Rust program in this template (you should a warm greeting): + + nix develop --command cargo run + + To build the Rust program using Nix: + + nix build + ''; }; }; }; From fc1c325b407381181dbdfe16467edb3c225d2051 Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Wed, 15 Oct 2025 00:51:27 -0300 Subject: [PATCH 7/8] Add note for stable/unstable Nixpkgs --- default/flake.nix | 2 +- nix-darwin/flake.nix | 6 +++--- nixos/flake.nix | 2 +- rust/flake.nix | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/default/flake.nix b/default/flake.nix index 89f3c99..decca6b 100644 --- a/default/flake.nix +++ b/default/flake.nix @@ -2,7 +2,7 @@ description = "An empty flake template that you can adapt to your own environment"; # Flake inputs - inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs + inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs (use 0.1 for unstable) # Flake outputs outputs = diff --git a/nix-darwin/flake.nix b/nix-darwin/flake.nix index dd8f3e0..5223a07 100644 --- a/nix-darwin/flake.nix +++ b/nix-darwin/flake.nix @@ -3,14 +3,14 @@ # Flake inputs inputs = { - # Stable Nixpkgs + # Stable Nixpkgs (use 0.1 for unstable) nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; - # Stable nix-darwin + # Stable nix-darwin (use 0.1 for unstable) nix-darwin = { url = "https://flakehub.com/f/nix-darwin/nix-darwin/0"; inputs.nixpkgs.follows = "nixpkgs"; }; - # Determinate module + # Determinate 3.* module determinate = { url = "https://flakehub.com/f/DeterminateSystems/determinate/3"; inputs.nixpkgs.follows = "nixpkgs"; diff --git a/nixos/flake.nix b/nixos/flake.nix index 449550d..09b8a00 100644 --- a/nixos/flake.nix +++ b/nixos/flake.nix @@ -3,7 +3,7 @@ # Flake inputs inputs = { - nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs + nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs (use 0.1 for unstable) determinate = { url = "https://flakehub.com/f/DeterminateSystems/determinate/3"; # Determinate 3.* inputs.nixpkgs.follows = "nixpkgs"; diff --git a/rust/flake.nix b/rust/flake.nix index f791092..4d4af37 100644 --- a/rust/flake.nix +++ b/rust/flake.nix @@ -2,7 +2,7 @@ description = "A Rust flake template that you can adapt to your own environment"; # Flake inputs - inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs + inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0"; # Stable Nixpkgs (use 0.1 for unstable) # Flake outputs outputs = From 9807c99da38f945c26e904dc3d2947662bca0ce6 Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Wed, 15 Oct 2025 11:12:55 -0300 Subject: [PATCH 8/8] Add Nix formatter --- rust/flake.nix | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust/flake.nix b/rust/flake.nix index 4d4af37..65960eb 100644 --- a/rust/flake.nix +++ b/rust/flake.nix @@ -88,5 +88,16 @@ }; } ); + + # Nix formatter + + # This applies the formatter that follows RFC 166, which defines a standard format: + # https://github.com/NixOS/rfcs/pull/166 + + # To format all Nix files: + # git ls-files -z '*.nix' | xargs -0 -r nix fmt + # To check formatting: + # git ls-files -z '*.nix' | xargs -0 -r nix develop --command nixfmt --check + formatter = forEachSupportedSystem ({ pkgs, ... }: pkgs.nixfmt-rfc-style); }; }