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 ✅" 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/flake.nix b/flake.nix index 0f66bf1..727f710 100644 --- a/flake.nix +++ b/flake.nix @@ -57,6 +57,24 @@ https://search.nixos.org/options ''; }; + + rust = { + description = "A flake template for Rust"; + path = ./rust; + 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 + ''; + }; }; }; } 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/.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..65960eb --- /dev/null +++ b/rust/flake.nix @@ -0,0 +1,103 @@ +{ + 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 (use 0.1 for unstable) + + # 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; + }; + } + ); + 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; [ + 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` + # 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 + env = { + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + }; + + # Add any shell logic you want executed when the environment is activated + shellHook = '' + echo "Rust toolchain 🦀" + cargo --version + ''; + }; + } + ); + + # Package outputs + packages = forEachSupportedSystem ( + { pkgs }: + { + # 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; + }; + } + ); + + # 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); + }; +} 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!"); +}