Attempt to get basic leptos site working in devshell and nix package
Some checks failed
/ dev-shell (push) Successful in 6m23s
/ check (push) Failing after 7m19s
/ rust-packages (push) Successful in 11m54s
/ terraform-providers (push) Successful in 5m32s
/ systems (push) Successful in 56m18s

This commit is contained in:
Kaare Hoff Skovgaard 2025-08-12 00:36:38 +02:00
parent 0ff2b12fb0
commit af3c61d75c
Signed by: khs
GPG key ID: C7D890804F01E9F0
19 changed files with 3051 additions and 2 deletions

9
.helix/languages.toml Normal file
View file

@ -0,0 +1,9 @@
[[language]]
name = "rust"
[language-server.rust-analyzer]
config = { procMacro = { ignored = { leptos_macro = [
# Optional:
# "component",
"server"
] } } }

9
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,9 @@
{
"rust-analyzer.procMacro.ignored": {
"leptos_macro": [
// optional:
// "component",
"server"
]
}
}

1
mx-aliases/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

2716
mx-aliases/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

54
mx-aliases/Cargo.toml Normal file
View file

@ -0,0 +1,54 @@
[package]
name = "mx-aliases"
edition = "2024"
version = "1.0.0"
metadata.crane.name = "mx-aliases"
[package.metadata.leptos]
output-name = "mx-aliases"
site-root = "target/mx-aliases"
site-pkg-dir = "assets"
style-file = "styles/main.scss"
assets-dir = "public"
site-addr = "127.0.0.1:3000"
reload-port = 3001
bin-features = ["server"]
bin-default-features = false
lib-features = ["client"]
lib-default-features = false
lib-profile-release = "wasm-release"
[profile.wasm-release]
inherits = "release"
opt-level = 'z'
lto = true
codegen-units = 1
panic = "abort"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["server", "client"]
server = ["ssr"]
client = ["hydrate"]
hydrate = ["leptos/hydrate", "dep:console_error_panic_hook", "dep:wasm-bindgen"]
ssr = [
"dep:axum",
"dep:tokio",
"dep:leptos_axum",
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
]
[dependencies]
axum = { version = "0.8.0", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
leptos = { version = "0.8.6" }
leptos_axum = { version = "0.8.0", optional = true }
leptos_meta = { version = "0.8.0" }
leptos_router = { version = "0.8.0" }
tokio = { version = "1", features = ["rt-multi-thread"], optional = true }
wasm-bindgen = { version = "=0.2.100", optional = true }

104
mx-aliases/default.nix Normal file
View file

@ -0,0 +1,104 @@
{
pkgs,
lib,
crane,
advisory-db,
}:
let
runtimeInputs = [ ];
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
src = craneLib.cleanCargoSource ./.;
commonArgs = {
inherit src;
strictDeps = true;
buildInputs = [ ];
nativeBuildInputs = [
pkgs.leptosfmt
pkgs.cargo-leptos
pkgs.binaryen
pkgs.dart-sass
];
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
individualCrateArgs = commonArgs // {
inherit cargoArtifacts;
inherit (craneLib.crateNameFromCargoToml { inherit src; }) version;
doCheck = false;
};
fileSet = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./Cargo.lock
./Cargo.toml
./rust-analyzer.toml
./rust-toolchain.toml
./rustfmt.toml
./styles
./src
./public
];
};
in
{
rustPackage = craneLib.buildPackage (
individualCrateArgs
// {
pname = "mx-aliases";
src = fileSet;
nativeBuildInputs = commonArgs.nativeBuildInputs ++ [ pkgs.makeWrapper ];
buildPhaseCargoCommand = ''
cargoBuildLog=$(mktemp cargoBuildLogXXXX.json)
cargo leptos build --release > "$cargoBuildLog"
'';
installPhase = ''
mkdir -p $out/bin/
cp target/release/mx-aliases $out/bin/
cp -r target/mx-aliases $out/lib
'';
postFixup = ''
wrapProgram $out/bin/mx-aliases \
--set PATH "${lib.makeBinPath runtimeInputs}" \
--set LEPTOS_SITE_ROOT $out/lib \
--set LEPTOS_SITE_PKG_DIR assets
'';
meta = {
mainProgram = "mx-aliases";
};
}
);
checks = {
mx-aliases-clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
mx-aliases-doc = craneLib.cargoDoc (
commonArgs
// {
inherit cargoArtifacts;
}
);
mx-aliases-fmt = craneLib.cargoFmt (
commonArgs
// {
inherit cargoArtifacts;
}
);
# Not used currently, as I have some other formatter changing toml formatting
# not sure where it comes from, but I need to find out and decide on a formatter
mx-aliases-toml-fmt = craneLib.taploFmt {
src = lib.sources.sourceFilesBySuffices src [ ".toml" ];
};
mx-aliases-audit = craneLib.cargoAudit {
inherit src advisory-db;
};
};
devDeps = [
rustToolchain
]
++ commonArgs.buildInputs
++ commonArgs.nativeBuildInputs;
}

0
mx-aliases/public/.gitignore vendored Normal file
View file

View file

@ -0,0 +1,2 @@
[rustfmt]
overrideCommand = ["leptosfmt", "--stdin", "--rustfmt"]

View file

@ -0,0 +1,10 @@
[toolchain]
channel = "1.88.0"
components = ["rustfmt", "clippy", "cargo", "rust-src"]
profile = "minimal"
targets = [
"wasm32-unknown-unknown",
"x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",
"aarch64-apple-darwin",
]

1
mx-aliases/rustfmt.toml Normal file
View file

@ -0,0 +1 @@
edition = "2024"

61
mx-aliases/src/app.rs Normal file
View file

@ -0,0 +1,61 @@
use leptos::prelude::*;
use leptos_meta::{MetaTags, Stylesheet, Title, provide_meta_context};
use leptos_router::{
StaticSegment,
components::{Route, Router, Routes},
};
pub fn shell(options: LeptosOptions) -> impl IntoView {
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<AutoReload options=options.clone() />
<HydrationScripts options />
<MetaTags />
</head>
<body>
<App />
</body>
</html>
}
}
#[component]
pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context();
view! {
// injects a stylesheet into the document <head>
// id=leptos means cargo-leptos will hot-reload this stylesheet
<Stylesheet id="leptos" href="/assets/mx-aliases.css" />
// sets the document title
<Title text="Welcome to Leptos" />
// content for this welcome page
<Router>
<main>
<Routes fallback=|| "Page not found.".into_view()>
<Route path=StaticSegment("") view=HomePage />
</Routes>
</main>
</Router>
}
}
/// Renders the home page of your application.
#[component]
fn HomePage() -> impl IntoView {
// Creates a reactive value to update the button
let count = RwSignal::new(0);
let on_click = move |_| *count.write() += 1;
view! {
<h1>"Welcome to Leptos!"</h1>
<button on:click=on_click>"Click Me: " {count}</button>
}
}

9
mx-aliases/src/lib.rs Normal file
View file

@ -0,0 +1,9 @@
pub mod app;
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
use crate::app::*;
console_error_panic_hook::set_once();
leptos::mount::hydrate_body(App);
}

38
mx-aliases/src/main.rs Normal file
View file

@ -0,0 +1,38 @@
#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
use axum::Router;
use leptos::logging::log;
use leptos::prelude::*;
use leptos_axum::{LeptosRoutes, generate_route_list};
use mx_aliases::app::*;
let conf = get_configuration(None).unwrap();
let addr = conf.leptos_options.site_addr;
let leptos_options = conf.leptos_options;
// Generate the list of routes in your Leptos App
let routes = generate_route_list(App);
let app = Router::new()
.leptos_routes(&leptos_options, routes, {
let leptos_options = leptos_options.clone();
move || shell(leptos_options.clone())
})
.fallback(leptos_axum::file_and_error_handler(shell))
.with_state(leptos_options);
// run our app with hyper
// `axum::Server` is a re-export of `hyper::Server`
log!("listening on http://{}", &addr);
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
#[cfg(not(feature = "ssr"))]
pub fn main() {
// no client-side main function
// unless we want this to work with e.g., Trunk for pure client-side testing
// see lib.rs for hydration function instead
}

View file

View file

@ -0,0 +1,11 @@
{
pkgs,
lib,
inputs,
...
}:
(import "${inputs.self}/mx-aliases" {
inherit lib pkgs;
crane = inputs.crane;
advisory-db = inputs.advisory-db;
}).rustPackage

View file

@ -8,5 +8,11 @@ mkShell {
packages = [ packages = [
pkgs.nixd pkgs.nixd
pkgs.nixfmt-rfc-style pkgs.nixfmt-rfc-style
] ++ (lib.khscodes.mkRust pkgs "${inputs.self}/rust").devDeps; ]
++ (lib.khscodes.mkRust pkgs "${inputs.self}/rust").devDeps
++ (import "${inputs.self}/mx-aliases" {
inherit lib pkgs;
crane = inputs.crane;
advisory-db = inputs.advisory-db;
}).devDeps;
} }

11
rust/.cargo/config.toml Normal file
View file

@ -0,0 +1,11 @@
[target.aarch64-apple-darwin]
rustflags = ["--cfg", "erase_components"]
[target.aarch64-unknown-linux-gnu]
rustflags = ["--cfg", "erase_components"]
[target.x86_64-unknown-linux-gnu]
rustflags = ["--cfg", "erase_components"]
[target.wasm32-unknown-unknown]
rustflags = ["--cfg", "erase_components"]

View file

@ -107,5 +107,6 @@ in
devDeps = [ devDeps = [
pkgs.cargo-hakari pkgs.cargo-hakari
rustToolchain rustToolchain
]; ]
++ commonArgs.buildInputs;
} }

View file

@ -2,3 +2,9 @@
channel = "1.88.0" channel = "1.88.0"
components = ["rustfmt", "clippy", "cargo", "rust-src"] components = ["rustfmt", "clippy", "cargo", "rust-src"]
profile = "minimal" profile = "minimal"
targets = [
"wasm32-unknown-unknown",
"x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",
"aarch64-apple-darwin",
]