From c82cd9830282fbbe417da1c53e9c3d1006d6ca42 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Thu, 2 May 2024 20:39:19 +0200 Subject: [PATCH] Update flake to add package --- .cargo/config.toml | 3 -- .gitignore | 1 + Cargo.lock | 2 +- Cargo.toml | 2 +- flake.lock | 105 ++++++++++++++++++++---------------------- flake.nix | 108 +++++++++++++++++++++++++++++--------------- rust-toolchain.toml | 3 ++ src/main.rs | 2 +- treefmt.nix | 8 ++++ 9 files changed, 135 insertions(+), 99 deletions(-) delete mode 100644 .cargo/config.toml create mode 100644 rust-toolchain.toml create mode 100644 treefmt.nix diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index d29d6c3..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,3 +0,0 @@ -[target.x86_64-unknown-linux-gnu] -linker = "clang" -rustflags = ["-C", "link-arg=-fuse-ld=mold"] diff --git a/.gitignore b/.gitignore index 2d5df85..bc9cf54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target .direnv +result \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 16715ab..c246035 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,7 +296,7 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "minesweeper-nojs" -version = "0.1.2" +version = "0.1.3" dependencies = [ "http-body-util", "hyper", diff --git a/Cargo.toml b/Cargo.toml index 4b9f189..c7166c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minesweeper-nojs" -version = "0.1.2" +version = "0.1.3" edition = "2021" [profile.release] diff --git a/flake.lock b/flake.lock index 703229a..cc6d304 100644 --- a/flake.lock +++ b/flake.lock @@ -1,92 +1,83 @@ { "nodes": { - "flake-utils": { + "fenix": { "inputs": { - "systems": "systems" + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1681202837, - "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "lastModified": 1714631076, + "narHash": "sha256-at4+1R9gx3CGvX0ZJo9GwDZyt3RzOft7qDCTsYHjI4M=", + "owner": "nix-community", + "repo": "fenix", + "rev": "22a9eb3f20dd340d084cee4426f386a90b1351ca", "type": "github" }, "original": { - "owner": "numtide", - "repo": "flake-utils", + "owner": "nix-community", + "repo": "fenix", "type": "github" } }, "nixpkgs": { "locked": { - "lastModified": 1703499205, - "narHash": "sha256-lF9rK5mSUfIZJgZxC3ge40tp1gmyyOXZ+lRY3P8bfbg=", - "owner": "NixOS", + "lastModified": 1714253743, + "narHash": "sha256-mdTQw2XlariysyScCv2tTE45QSU9v/ezLcHJ22f0Nxc=", + "owner": "nixos", "repo": "nixpkgs", - "rev": "e1fa12d4f6c6fe19ccb59cac54b5b3f25e160870", + "rev": "58a1abdbae3217ca6b702f03d3b35125d88a2994", "type": "github" }, "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1681358109, - "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", + "owner": "nixos", + "ref": "nixos-unstable", "repo": "nixpkgs", "type": "github" } }, "root": { "inputs": { + "fenix": "fenix", "nixpkgs": "nixpkgs", - "rust-overlay": "rust-overlay" + "treefmt-nix": "treefmt-nix" } }, - "rust-overlay": { + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1714572655, + "narHash": "sha256-xjD8vmit0Nz1qaSSSpeXOK3saSvAZtOGHS2SHZE75Ek=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "cfce2bb46da62950a8b70ddb0b2a12332da1b1e1", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "treefmt-nix": { "inputs": { - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs_2" + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { - "lastModified": 1703902408, - "narHash": "sha256-qXdWvu+tlgNjeoz8yQMRKSom6QyRROfgpmeOhwbujqw=", - "owner": "oxalica", - "repo": "rust-overlay", - "rev": "319f57cd2c34348c55970a4bf2b35afe82088681", + "lastModified": 1714058656, + "narHash": "sha256-Qv4RBm4LKuO4fNOfx9wl40W2rBbv5u5m+whxRYUMiaA=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "c6aaf729f34a36c445618580a9f95a48f5e4e03f", "type": "github" }, "original": { - "owner": "oxalica", - "repo": "rust-overlay", - "type": "github" - } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", + "owner": "numtide", + "repo": "treefmt-nix", "type": "github" } } diff --git a/flake.nix b/flake.nix index 12d2a48..2e81b1d 100644 --- a/flake.nix +++ b/flake.nix @@ -2,47 +2,83 @@ description = "A Nix-flake-based Rust development environment"; inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - rust-overlay.url = "github:oxalica/rust-overlay"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; - outputs = { self, nixpkgs, rust-overlay }: + outputs = + { + self, + nixpkgs, + treefmt-nix, + fenix, + }: let - overlays = [ - rust-overlay.overlays.default - (final: prev: { - rustToolchain = - let - rust = prev.rust-bin; - in - if builtins.pathExists ./rust-toolchain.toml then - rust.fromRustupToolchainFile ./rust-toolchain.toml - else if builtins.pathExists ./rust-toolchain then - rust.fromRustupToolchainFile ./rust-toolchain - else - rust.stable.latest.default; - }) + supportedSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" ]; - supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; - forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f { - pkgs = import nixpkgs { inherit overlays system; }; - }); + forEachSupportedSystem = + f: + nixpkgs.lib.genAttrs supportedSystems ( + system: + f ( + let + pkgs = import nixpkgs { inherit system; }; + fenix' = (import fenix { inherit system pkgs; }); + toolchain = fenix'.complete.withComponents ( + (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml)).toolchain.components + ); + in + { + inherit pkgs toolchain; + } + ) + ); in { - devShells = forEachSupportedSystem ({ pkgs }: { - default = pkgs.mkShell { - packages = with pkgs; [ - mold - clang - rustToolchain - openssl - pkg-config - cargo-deny - cargo-edit - cargo-watch - rust-analyzer - ]; - }; - }); + formatter = forEachSupportedSystem ( + { pkgs, ... }: (treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.wrapper + ); + devShells = forEachSupportedSystem ( + { pkgs, toolchain }: + { + default = pkgs.mkShell { + nativeBuildInputs = [ toolchain ]; + buildInputs = [ toolchain ]; + }; + } + ); + packages = forEachSupportedSystem ( + { pkgs, toolchain }: + { + default = ( + let + manifest = (pkgs.lib.importTOML ./Cargo.toml).package; + rustPlatform = pkgs.makeRustPlatform { + cargo = toolchain; + rustc = toolchain; + }; + in + rustPlatform.buildRustPackage { + pname = manifest.name; + version = manifest.version; + cargoLock.lockFile = ./Cargo.lock; + src = pkgs.lib.cleanSource ./.; + } + ); + } + ); }; } diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..7fd885e --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "stable" +components = ["rustfmt", "cargo", "rustc", "rust-src"] diff --git a/src/main.rs b/src/main.rs index d81c941..1bd3130 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ use tokio::net::TcpListener; // Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia. // // https://github.com/rust-lang/rust/blob/1.55.0/library/core/src/slice/sort.rs#L559-L573 -pub fn random_numbers() -> impl Iterator { +pub fn random_numbers() -> std::iter::RepeatWith usize> { let mut random = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap_or(Duration::new(0, 0)) diff --git a/treefmt.nix b/treefmt.nix new file mode 100644 index 0000000..a54f850 --- /dev/null +++ b/treefmt.nix @@ -0,0 +1,8 @@ +{ ... }: +{ + projectRootFile = "flake.nix"; + programs = { + nixfmt-rfc-style.enable = true; + rustfmt.enable = true; + }; +}