From af394b315e3f344f054a631cc827db9c4cb2c14b Mon Sep 17 00:00:00 2001 From: Colin Date: Wed, 26 Apr 2023 07:20:02 +0000 Subject: [PATCH] add a flake template for rust binaries --- flake.nix | 6 ++++ templates/rust/Cargo.lock | 7 +++++ templates/rust/Cargo.toml | 8 +++++ templates/rust/flake.lock | 60 ++++++++++++++++++++++++++++++++++++++ templates/rust/flake.nix | 45 ++++++++++++++++++++++++++++ templates/rust/src/main.rs | 5 ++++ 6 files changed, 131 insertions(+) create mode 100644 templates/rust/Cargo.lock create mode 100644 templates/rust/Cargo.toml create mode 100644 templates/rust/flake.lock create mode 100644 templates/rust/flake.nix create mode 100644 templates/rust/src/main.rs diff --git a/flake.nix b/flake.nix index 05d91d8cc..14c724bd0 100644 --- a/flake.nix +++ b/flake.nix @@ -290,6 +290,12 @@ path = ./templates/python-data; description = "python environment for data processing"; }; + rust = { + # initialize with: + # - `nix flake init -t '/home/colin/dev/nixos/#rust'` + path = ./templates/rust; + description = "rust package and development environment"; + }; }; }; } diff --git a/templates/rust/Cargo.lock b/templates/rust/Cargo.lock new file mode 100644 index 000000000..766f2080f --- /dev/null +++ b/templates/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "example-rust-pkg" +version = "0.1.0" diff --git a/templates/rust/Cargo.toml b/templates/rust/Cargo.toml new file mode 100644 index 000000000..fafb967bf --- /dev/null +++ b/templates/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "example-rust-pkg" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/templates/rust/flake.lock b/templates/rust/flake.lock new file mode 100644 index 000000000..f3083a0c7 --- /dev/null +++ b/templates/rust/flake.lock @@ -0,0 +1,60 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1682173319, + "narHash": "sha256-tPhOpJJ+wrWIusvGgIB2+x6ILfDkEgQMX0BTtM5vd/4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ee7ec1c71adc47d2e3c2d5eb0d6b8fbbd42a8d1c", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-22.11", + "type": "indirect" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/templates/rust/flake.nix b/templates/rust/flake.nix new file mode 100644 index 000000000..ba9593c52 --- /dev/null +++ b/templates/rust/flake.nix @@ -0,0 +1,45 @@ +{ + description = "TODO: FILLME: description"; + inputs = { + nixpkgs.url = "nixpkgs/nixos-22.11"; + flake-utils.url = github:numtide/flake-utils; + }; + + outputs = { self, nixpkgs, flake-utils }: + with flake-utils.lib; eachSystem allSystems (system: + let + pkgs = import nixpkgs { + inherit system; + }; + nativeBuildInputs = with pkgs; [ + pkg-config + ]; + buildInputs = with pkgs; [ + openssl + ]; + in rec { + packages = { + # docs: /doc/languages-frameworks/rust.section.md + example-rust-pkg = pkgs.rustPlatform.buildRustPackage { + name = "example-rust-pkg"; + src = ./.; + cargoLock.lockFile = ./Cargo.lock; + # enables debug builds, if we want: https://github.com/NixOS/nixpkgs/issues/60919. + hardeningDisable = [ "fortify" ]; + inherit buildInputs nativeBuildInputs; + }; + }; + defaultPackage = packages.example-rust-pkg; + + devShells.default = with pkgs; mkShell { + # enables debug builds, if we want: https://github.com/NixOS/nixpkgs/issues/60919. + hardeningDisable = [ "fortify" ]; + + # Allow cargo to download crates. + SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; + + inherit buildInputs; + nativeBuildInputs = [ cargo ] ++ nativeBuildInputs; + }; + }); +} diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs new file mode 100644 index 000000000..e391eae6b --- /dev/null +++ b/templates/rust/src/main.rs @@ -0,0 +1,5 @@ +//! hello world + +pub fn main() { + println!("hello, world!"); +}