From 3a36a870b79147dbbc2ede6904bae497e0810062 Mon Sep 17 00:00:00 2001 From: "Colton J. McCurdy" Date: Sun, 15 Oct 2023 16:24:08 -0400 Subject: [PATCH] cargo build works with nix shell now Signed-off-by: Colton J. McCurdy --- .gitignore | 1 + flake.lock | 260 ++++------------------------------------------------- flake.nix | 125 ++++++++------------------ shell.nix | 26 ++++++ 4 files changed, 78 insertions(+), 334 deletions(-) create mode 100644 shell.nix diff --git a/.gitignore b/.gitignore index 895f7d5..9fb0624 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target .devenv .direnv +result diff --git a/flake.lock b/flake.lock index 0fb9f1e..c9571ea 100644 --- a/flake.lock +++ b/flake.lock @@ -1,88 +1,15 @@ { "nodes": { - "devenv": { - "inputs": { - "flake-compat": "flake-compat", - "nix": "nix", - "nixpkgs": "nixpkgs", - "pre-commit-hooks": "pre-commit-hooks" - }, - "locked": { - "lastModified": 1683288106, - "narHash": "sha256-8/yuP6gWLhK8tRCtyqY5QnTq9GF7pCWpZyyZ08lXFwM=", - "owner": "cachix", - "repo": "devenv", - "rev": "c4006ccba1b3e4533de462cee5933e0ccf5f1d6a", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "devenv", - "type": "github" - } - }, - "fenix": { - "inputs": { - "nixpkgs": [ - "nixpkgs" - ], - "rust-analyzer-src": "rust-analyzer-src" - }, - "locked": { - "lastModified": 1683613339, - "narHash": "sha256-QSQtAOeIy1XabNXCkpk4E1Gz14Oqolkvp6wtaIZ3dkQ=", - "owner": "nix-community", - "repo": "fenix", - "rev": "55a548eaef105815905977b8470ee35c67e67b7b", - "type": "github" - }, - "original": { - "owner": "nix-community", - "repo": "fenix", - "type": "github" - } - }, - "flake-compat": { - "flake": false, - "locked": { - "lastModified": 1673956053, - "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", - "owner": "edolstra", - "repo": "flake-compat", - "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", - "type": "github" - }, - "original": { - "owner": "edolstra", - "repo": "flake-compat", - "type": "github" - } - }, "flake-utils": { - "locked": { - "lastModified": 1667395993, - "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "flake-utils_2": { "inputs": { "systems": "systems" }, "locked": { - "lastModified": 1681202837, - "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", "owner": "numtide", "repo": "flake-utils", - "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", "type": "github" }, "original": { @@ -91,7 +18,7 @@ "type": "github" } }, - "flake-utils_3": { + "flake-utils_2": { "inputs": { "systems": "systems_2" }, @@ -109,133 +36,23 @@ "type": "github" } }, - "gitignore": { - "inputs": { - "nixpkgs": [ - "devenv", - "pre-commit-hooks", - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1660459072, - "narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=", - "owner": "hercules-ci", - "repo": "gitignore.nix", - "rev": "a20de23b925fd8264fd7fad6454652e142fd7f73", - "type": "github" - }, - "original": { - "owner": "hercules-ci", - "repo": "gitignore.nix", - "type": "github" - } - }, - "lowdown-src": { - "flake": false, - "locked": { - "lastModified": 1633514407, - "narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=", - "owner": "kristapsdz", - "repo": "lowdown", - "rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8", - "type": "github" - }, - "original": { - "owner": "kristapsdz", - "repo": "lowdown", - "type": "github" - } - }, - "nix": { - "inputs": { - "lowdown-src": "lowdown-src", - "nixpkgs": [ - "devenv", - "nixpkgs" - ], - "nixpkgs-regression": "nixpkgs-regression" - }, - "locked": { - "lastModified": 1676545802, - "narHash": "sha256-EK4rZ+Hd5hsvXnzSzk2ikhStJnD63odF7SzsQ8CuSPU=", - "owner": "domenkozar", - "repo": "nix", - "rev": "7c91803598ffbcfe4a55c44ac6d49b2cf07a527f", - "type": "github" - }, - "original": { - "owner": "domenkozar", - "ref": "relaxed-flakes", - "repo": "nix", - "type": "github" - } - }, "nixpkgs": { "locked": { - "lastModified": 1678875422, - "narHash": "sha256-T3o6NcQPwXjxJMn2shz86Chch4ljXgZn746c2caGxd8=", + "lastModified": 1697059129, + "narHash": "sha256-9NJcFF9CEYPvHJ5ckE8kvINvI84SZZ87PvqMbH6pro0=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "126f49a01de5b7e35a43fd43f891ecf6d3a51459", + "rev": "5e4c2ada4fcd54b99d56d7bd62f384511a7e2593", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs-regression": { - "locked": { - "lastModified": 1643052045, - "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", - "type": "github" - }, - "original": { - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", - "type": "github" - } - }, - "nixpkgs-stable": { - "locked": { - "lastModified": 1678872516, - "narHash": "sha256-/E1YwtMtFAu2KUQKV/1+KFuReYPANM2Rzehk84VxVoc=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "9b8e5abb18324c7fe9f07cb100c3cd4a29cda8b8", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-22.11", + "ref": "nixos-unstable", "repo": "nixpkgs", "type": "github" } }, "nixpkgs_2": { - "locked": { - "lastModified": 1683546353, - "narHash": "sha256-zEfx6q4fXK5wq+RCsyM0FyWBKNNhHTKMpCWkEpKDe2g=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "7629f9b0680d87c7775f3261bee746da5dac76d1", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-22.11", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_3": { "locked": { "lastModified": 1681358109, "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", @@ -251,72 +68,25 @@ "type": "github" } }, - "pre-commit-hooks": { - "inputs": { - "flake-compat": [ - "devenv", - "flake-compat" - ], - "flake-utils": "flake-utils", - "gitignore": "gitignore", - "nixpkgs": [ - "devenv", - "nixpkgs" - ], - "nixpkgs-stable": "nixpkgs-stable" - }, - "locked": { - "lastModified": 1682596858, - "narHash": "sha256-Hf9XVpqaGqe/4oDGr30W8HlsWvJXtMsEPHDqHZA6dDg=", - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "rev": "fb58866e20af98779017134319b5663b8215d912", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "type": "github" - } - }, "root": { "inputs": { - "devenv": "devenv", - "fenix": "fenix", - "flake-utils": "flake-utils_2", - "nixpkgs": "nixpkgs_2", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", "rust-overlay": "rust-overlay", "systems": "systems_3" } }, - "rust-analyzer-src": { - "flake": false, - "locked": { - "lastModified": 1683571499, - "narHash": "sha256-SUs1qlsGJB09yjAKLQIJVxUPHGUdcayzE9IOkV4XRFM=", - "owner": "rust-lang", - "repo": "rust-analyzer", - "rev": "c26a43d6bd660eba94500645a47f931e153015d8", - "type": "github" - }, - "original": { - "owner": "rust-lang", - "ref": "nightly", - "repo": "rust-analyzer", - "type": "github" - } - }, "rust-overlay": { "inputs": { - "flake-utils": "flake-utils_3", - "nixpkgs": "nixpkgs_3" + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1683598806, - "narHash": "sha256-L0tcBTcF6QzFFvWbXO2TXlBszAvxvm3zblYe0iUF/Yk=", + "lastModified": 1697336027, + "narHash": "sha256-ctmmw7j4liyfSh63v9rdFZeIoNYCkCvgqvtEOB7KhX8=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "0cef0c3ed57980dc9a8b916e6b596d7f2f6d34f1", + "rev": "e494404d36a41247987eeb1bfc2f1ca903e97764", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index c4dd5ef..2ad9da9 100644 --- a/flake.nix +++ b/flake.nix @@ -1,101 +1,48 @@ -# This is devenv, but with Flakes - https://devenv.sh/guides/using-with-flakes/ -# And is configured so that it automatically enters a nix-shell via nix-direnv -# when you change into the directory. -# # https://www.tweag.io/blog/2022-09-22-rust-nix/ +# https://ryantm.github.io/nixpkgs/languages-frameworks/rust/ { inputs = { - # I ran into issues with "version 'GLIBC_ABI_DT_RELR' not found" when I set to "unstable" - nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; systems.url = "github:nix-systems/default"; flake-utils.url = "github:numtide/flake-utils"; rust-overlay.url = "github:oxalica/rust-overlay"; - devenv.url = "github:cachix/devenv"; - - # https://devenv.sh/blog/2022/12/22/devenv-05/#languages - # Necessary for specifying "stable" for language.rust.version. - fenix = { - url = "github:nix-community/fenix"; - inputs.nixpkgs.follows = "nixpkgs"; - }; }; - outputs = { self, nixpkgs, devenv, flake-utils, rust-overlay, systems, ... } @ inputs: - flake-utils.lib.eachDefaultSystem (system: - let - overlays = [ (import rust-overlay) ]; - pkgs = import nixpkgs { inherit system overlays; }; - rustVersion = pkgs.rust-bin.stable.latest.default; - - rustPlatform = pkgs.makeRustPlatform { - cargo = rustVersion; - rustc = rustVersion; - }; - - rsBuild = rustPlatform.buildRustPackage { - pname = "gitrs"; - version = "0.1.0"; - src = ./.; - - # https://dev.to/johnreillymurray/rust-environment-and-docker-build-with-nix-flakes-19c1 - # just for the host building the package - nativeBuildInputs = [ - pkgs.pkg-config - pkgs.cmake - pkgs.clang - pkgs.gcc - pkgs.openssl - pkgs.zlib - ]; - # packages needed by the consumer - buildInputs = [ ]; - - cargoLock = { - lockFile = ./Cargo.lock; - }; - - PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; - RUST_BACKTRACE = "full"; + outputs = { + self, + nixpkgs, + flake-utils, + rust-overlay, + systems, + ... + } @ inputs: + flake-utils.lib.eachDefaultSystem (system: let + overlays = [(import rust-overlay)]; + pkgs = import nixpkgs {inherit system overlays;}; + rustVersion = pkgs.rust-bin.stable.latest.default; + + rustPlatform = pkgs.makeRustPlatform { + cargo = rustVersion; + rustc = rustVersion; + }; + + rsBuild = rustPlatform.buildRustPackage { + pname = "gitrs"; + version = "v0.3.6"; + + src = ./.; + + cargoLock = { + lockFile = ./Cargo.lock; }; - in - { - defaultPackage = rsBuild; - - devShell = devenv.lib.mkShell - { - inherit inputs pkgs; - - # When devenv is used with Flakes, this is where you configure your - # devenv shells - # - # Docs: - # - https://nixos.wiki/wiki/Rust#devenv.sh_support - # - https://devenv.sh/reference/options/ - # - https://devenv.sh/guides/using-with-flakes/#modifying-your-flakenix-file - # - https://github.com/cachix/devenv/blob/main/src/modules/languages/rust.nix - modules = [ - { - languages.rust = { - enable = true; - version = "stable"; - }; - - # https://nixos.wiki/wiki/Rust#Building_Rust_crates_that_require_external_system_libraries - env.PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; + }; + in { + defaultPackage = rsBuild; - packages = [ - pkgs.openssl - pkgs.pkg-config - pkgs.vale - ]; + formatter = pkgs.alejandra; - # https://devenv.sh/reference/options/?query=rust#pre-commithooks - pre-commit.hooks = { - clippy.enable = true; - rustfmt.enable = true; - }; - } - ]; - }; - }); + devShells = { + default = import ./shell.nix {inherit pkgs;}; + }; + }); } diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..db0cf51 --- /dev/null +++ b/shell.nix @@ -0,0 +1,26 @@ +# https://nixos.wiki/wiki/Flakes#Super_fast_nix-shell +{pkgs ? import {}}: +with pkgs; + mkShell { + # https://nixos.wiki/wiki/Rust#Shell.nix_example + nativeBuildInputs = with pkgs; [ + alejandra + cargo + cargo-flamegraph + clippy + gcc + openssl + openssl.dev + pkg-config + rnix-lsp + rust-analyzer + rustc + rustfmt + statix + ]; + + # https://nixos.wiki/wiki/Rust#Building_Rust_crates_that_require_external_system_libraries + PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; + + RUST_BACKTRACE = 1; + }