From 86ec666da363a1b32610afc17d68953de85a1970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sun, 21 Jan 2024 14:56:06 +0100 Subject: [PATCH] refactor: improve flake --- .cargo/config.toml | 3 -- flake.lock | 18 +++++------ flake.nix | 78 ++++++++++++++++++++++++---------------------- rustfmt.toml | 1 + src/main.rs | 14 +++++---- 5 files changed, 58 insertions(+), 56 deletions(-) delete mode 100644 .cargo/config.toml create mode 100644 rustfmt.toml diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 3157afb..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,3 +0,0 @@ -[target.x86_64-unknown-linux-gnu] -linker = "clang" - diff --git a/flake.lock b/flake.lock index e3989d7..13e4d53 100644 --- a/flake.lock +++ b/flake.lock @@ -28,11 +28,11 @@ "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1705731714, - "narHash": "sha256-aMeN/ASG4n7RIIPLiy+txoMdDTvIcaRDX6acbeeRtEU=", + "lastModified": 1705818179, + "narHash": "sha256-eNlzk9S9MiV8h1Z4vYpNWHrgFh0wkJCoSPDLhMYEJhU=", "owner": "nix-community", "repo": "fenix", - "rev": "712f25ec7e1f5839d486b246a5afa5e31f5df6ff", + "rev": "40f11d5591036cd03dffaae9aea1d3fec3c62b54", "type": "github" }, "original": { @@ -61,11 +61,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1705496572, - "narHash": "sha256-rPIe9G5EBLXdBdn9ilGc0nq082lzQd0xGGe092R/5QE=", + "lastModified": 1705677747, + "narHash": "sha256-eyM3okYtMgYDgmYukoUzrmuoY4xl4FUujnsv/P6I/zI=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "842d9d80cfd4560648c785f8a4e6f3b096790e19", + "rev": "bbe7d8f876fbbe7c959c90ba2ae2852220573261", "type": "github" }, "original": { @@ -85,11 +85,11 @@ "rust-analyzer-src": { "flake": false, "locked": { - "lastModified": 1705697225, - "narHash": "sha256-eLMwix3LPsgqnbdLMWivBCSBrWnaAA50JtMNnInTopg=", + "lastModified": 1705767842, + "narHash": "sha256-lBvKPxeJ/UJPBxncu622hts0xbYfmo2GYu5E7K3MZt0=", "owner": "rust-lang", "repo": "rust-analyzer", - "rev": "67cfbf231c1e2ba3129529de950d1c4ca7921404", + "rev": "a9116523604c998e7781f60d3b5a6f586e0414a9", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 596090b..38bd842 100644 --- a/flake.nix +++ b/flake.nix @@ -1,53 +1,55 @@ { inputs = { - crane = { - url = "github:ipetkov/crane"; - inputs = { - flake-utils.follows = "flake-utils"; - nixpkgs.follows = "nixpkgs"; - }; - }; - fenix = { - url = "github:nix-community/fenix"; - inputs.nixpkgs.follows = "nixpkgs"; - }; + crane.url = "github:ipetkov/crane"; + crane.inputs.nixpkgs.follows = "nixpkgs"; + fenix.url = "github:nix-community/fenix"; + fenix.inputs.nixpkgs.follows = "nixpkgs"; flake-utils.url = "github:numtide/flake-utils"; nixpkgs.url = "nixpkgs/nixos-unstable"; }; - outputs = { self, crane, fenix, flake-utils, nixpkgs }: + outputs = { self, crane, flake-utils, nixpkgs, ... }@inputs: flake-utils.lib.eachDefaultSystem (system: let - pkgs = (import nixpkgs) { - inherit system; - }; - toolchain = fenix.packages.${system}.stable; + inherit (pkgs) lib; + pkgs = import nixpkgs { inherit system; }; + fenix = inputs.fenix.packages.${system}; craneLib = crane.lib.${system}.overrideToolchain toolchain.toolchain; + mkSrc = extraPaths: with lib.fileset; let + root = ./.; + rustFiles = fromSource (craneLib.cleanCargoSource root); + fileset = union rustFiles (unions extraPaths); + in + toSource { inherit root fileset; }; + + ## Customize here ## + toolchain = fenix.complete; # or fenix.stable; + stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv; in { - packages.default = - let - inherit (pkgs.lib) fileset; - in - craneLib.buildPackage { - src = fileset.toSource { - root = ./.; - fileset = fileset.intersection - (fileset.difference ./. - (fileset.unions [ ./.cargo ./flake.nix ./flake.lock ./.envrc ])) - (fileset.gitTracked ./.); - }; - }; + packages.default = craneLib.buildPackage { + inherit stdenv; + src = mkSrc [ ./templates ]; + strictDeps = true; + buildInputs = pkgs.stdenv.isDarwin [ + pkgs.libiconv + ]; + }; - devShell = pkgs.mkShell.override + devShells.default = pkgs.mkShell.override { inherit stdenv; } { - stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.clangStdenv; - } - { - nativeBuildInputs = with toolchain; - [ rustc cargo rust-analyzer clippy ] ++ (with pkgs; - [ entr ] - ); + nativeBuildInputs = with pkgs; [ + entr + htmx-lsp + ] ++ (with toolchain; [ + cargo + clippy + rustfmt + rustc + fenix.rust-analyzer + ]); + RUST_SRC_PATH = "${toolchain.rust-src}/lib/rustlib/src/rust/library"; }; - }); + } + ); } diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..3a26366 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +edition = "2021" diff --git a/src/main.rs b/src/main.rs index 0779dee..41980b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ use std::sync::Mutex; -use actix_web::{App, HttpServer}; use actix_web::web; +use actix_web::{App, HttpServer}; use askama::Template; #[derive(Debug)] struct AppState { - counter: Mutex + counter: Mutex, } #[derive(Template)] @@ -25,7 +25,9 @@ struct IndexTemplate { async fn index(data: web::Data) -> impl actix_web::Responder { let counter = data.counter.lock().unwrap(); let counter_template = CounterTemplate { counter: *counter }; - IndexTemplate { counter: counter_template } + IndexTemplate { + counter: counter_template, + } } #[actix_web::post("/api/increment")] @@ -47,10 +49,10 @@ async fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "debug"); env_logger::init(); println!("Starting webserver on port 8080"); - let app_state = web::Data::new( AppState { - counter: Mutex::new(0) + let app_state = web::Data::new(AppState { + counter: Mutex::new(0), }); - HttpServer::new(move|| { + HttpServer::new(move || { App::new() .app_data(app_state.clone()) .service(index)