refactor: improve flake

main
Moritz Böhme 2024-01-21 14:56:06 +01:00
parent 14b4baf6e3
commit 86ec666da3
Signed by: moritz
GPG Key ID: 970C6E89EB0547A9
5 changed files with 58 additions and 56 deletions

View File

@ -1,3 +0,0 @@
[target.x86_64-unknown-linux-gnu]
linker = "clang"

View File

@ -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": {

View File

@ -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";
};
});
}
);
}

1
rustfmt.toml Normal file
View File

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

View File

@ -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<i32>
counter: Mutex<i32>,
}
#[derive(Template)]
@ -25,7 +25,9 @@ struct IndexTemplate {
async fn index(data: web::Data<AppState>) -> 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)