Last active
January 19, 2025 12:16
-
-
Save geoffreygarrett/2885a82f4b8c478298c3a796bca4fcc2 to your computer and use it in GitHub Desktop.
Nix DevShell for Leptos with pinned Rust toolchains via Fenix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# 1. If .rust-channel exists, read it; otherwise default to stable | |
if [ -f .rust-channel ]; then | |
RUST_CHANNEL="$(cat .rust-channel)" | |
else | |
RUST_CHANNEL="stable" | |
fi | |
# 2. Validate the channel; if it's not "nightly", assume stable | |
if [ "$RUST_CHANNEL" != "nightly" ]; then | |
RUST_CHANNEL="stable" | |
fi | |
echo "Selected Rust channel: $RUST_CHANNEL" | |
# 3. Load the corresponding devShell from the flake | |
use flake .#$RUST_CHANNEL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
description = "DevShell for Leptos with pinned Rust toolchains via Fenix"; | |
inputs = { | |
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; | |
fenix.url = "github:nix-community/fenix"; | |
flake-utils.url = "github:numtide/flake-utils"; | |
}; | |
outputs = { self, fenix, nixpkgs, flake-utils, ... }: | |
flake-utils.lib.eachDefaultSystem (system: | |
let | |
# Load Nixpkgs + Fenix overlay | |
pkgs = import nixpkgs { | |
inherit system; | |
overlays = [ fenix.overlays.default ]; | |
}; | |
# Pinned stable & nightly Rust versions | |
stable-version = "1.84.0"; | |
stable-sha256 = "sha256-lMLAupxng4Fd9F1oDw8gx+qA0RuF7ou7xhNU8wgs0PU="; | |
nightly-version = "nightly-2025-01-19"; | |
nightly-sha256 = "sha256-1T+L08cNYmnZRtJT76gQRfKbwSkkNsY+Gks27mQWfS8="; | |
# Create pinned toolchains | |
pinned-stable = fenix.packages.${system}.fromToolchainName { | |
name = stable-version; | |
sha256 = stable-sha256; | |
}; | |
pinned-nightly = fenix.packages.${system}.fromToolchainName { | |
name = nightly-version; | |
sha256 = nightly-sha256; | |
}; | |
pinned-stable-toolchain = pinned-stable.withComponents [ | |
"cargo" "clippy" "rust-src" "rustc" "rustfmt" | |
]; | |
pinned-nightly-toolchain = pinned-nightly.withComponents [ | |
"cargo" "clippy" "rust-src" "rustc" "rustfmt" | |
]; | |
pinned-rust-analyzer = pkgs.rust-analyzer-nightly; | |
# Common Leptos and front-end build tools | |
common-build-inputs = [ | |
pkgs.openssl_3 | |
pkgs.pkg-config | |
pkgs.cacert | |
pkgs.cargo-make | |
pkgs.trunk | |
pkgs.tailwindcss | |
pkgs.dart-sass | |
pkgs.cargo-leptos | |
pkgs.cargo-generate | |
pkgs.wasm-pack | |
pkgs.wasm-bindgen-cli | |
pinned-rust-analyzer | |
]; | |
# Extra Darwin frameworks if needed | |
darwin-frameworks = pkgs.lib.optionals pkgs.stdenv.isDarwin [ | |
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration | |
]; | |
mk-leptos-shell = channel: pkgs.mkShell { | |
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [ pkgs.openssl_3 ]; | |
buildInputs = common-build-inputs | |
++ (if channel == "nightly" | |
then [ pinned-nightly-toolchain ] | |
else [ pinned-stable-toolchain ]) | |
++ darwin-frameworks; | |
# The shell hook is run at shell startup. We can export LD_LIBRARY_PATH here. | |
shellHook = '' | |
echo " 🦀 Welcome to ${channel} Rust! 🦀 " | |
echo "Happy hacking with Leptos!" | |
''; | |
}; | |
in { | |
devShells = { | |
default = mk-leptos-shell "stable"; | |
stable = mk-leptos-shell "stable"; | |
nightly = mk-leptos-shell "nightly"; | |
}; | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment