diff --git a/.gitignore b/.gitignore index c403c34..68211ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ /target .idea/ + +.env* + +# Nix stuff +.direnv/ +result* diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..1c812ba --- /dev/null +++ b/flake.lock @@ -0,0 +1,151 @@ +{ + "nodes": { + "crane": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ], + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1686621798, + "narHash": "sha256-FUwWszmSiDzUdTk8f69xwMoYlhdPaLvDaIYOE/y6VXc=", + "owner": "ipetkov", + "repo": "crane", + "rev": "75f7d715f8088f741be9981405f6444e2d49efdd", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "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": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1685518550, + "narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1686638277, + "narHash": "sha256-jfiMyVAFECmst2SKztxPaScrxKKxDNWPVWcWa13Wvs0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "f5f4bba439da59cc612b2f1cc5798b1960ec1add", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay_2" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "crane", + "flake-utils" + ], + "nixpkgs": [ + "crane", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1685759304, + "narHash": "sha256-I3YBH6MS3G5kGzNuc1G0f9uYfTcNY9NYoRc3QsykLk4=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "c535b4f3327910c96dcf21851bbdd074d0760290", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "rust-overlay_2": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1686623191, + "narHash": "sha256-x2gQcKtSgfbZlcTaVvdMPbrXMRjUEYIV88yzsFww6D4=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "e279547de84413ca1a65cec3f0f879709c8c65eb", + "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", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..d16c05d --- /dev/null +++ b/flake.nix @@ -0,0 +1,160 @@ +{ + description = "A Nix-flake-based Rust development environment"; + + inputs = { + crane = { + url = "github:ipetkov/crane"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-utils.follows = "flake-utils"; + }; + }; + flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:NixOS/nixpkgs"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-utils.follows = "flake-utils"; + }; + }; + }; + + outputs = { + self, + crane, + flake-utils, + nixpkgs, + rust-overlay, + }: + flake-utils.lib.eachDefaultSystem (system: let + pkgs = import nixpkgs { + inherit system; + overlays = [(import rust-overlay)]; + }; + + # # Target musl when building on 64-bit linux + # buildTarget = + # {"x86_64-linux" = "x86_64-unknown-linux-musl";}.${system} + # or (pkgs.rust.toRustTargetSpec pkgs.stdenv.hostPlatform); + # rustToolchain = pkgs.rust-bin.stable.latest.default.override { + # targets = [ + # buildTarget + # (pkgs.rust.toRustTargetSpec pkgs.stdenv.hostPlatform) + # ]; + # }; + rustToolchain = pkgs.rust-bin.stable.latest.default; + + # Set-up build dependencies and configure rust + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; + + inherit (pkgs) lib; + # Shamelessly stolen from: + # https://github.com/fedimint/fedimint/blob/66519d5e978e22bb10a045f406101891e1bb7eb5/flake.nix#L99 + filterSrcWithRegexes = regexes: src: let + basePath = toString src + "/"; + in + lib.cleanSourceWith { + filter = ( + path: type: let + relPath = lib.removePrefix basePath (toString path); + includePath = + (type == "directory") + || lib.any + (re: builtins.match re relPath != null) + regexes; + in + # uncomment to debug: + # builtins.trace "${relPath}: ${lib.boolToString includePath}" + includePath + ); + inherit src; + }; + + cargo-details = lib.importTOML ./Cargo.toml; + pname = cargo-details.package.name; + commonArgs = { + nativeBuildInputs = with pkgs; [ + glib + openssl + pkg-config + ]; + # CARGO_BUILD_TARGET = buildTarget; + }; + + # Compile and cache only cargo dependencies + dep-files-filter = ["Cargo.lock" "Cargo.toml" ".*/Cargo.toml"]; + cargo-deps = craneLib.buildDepsOnly (commonArgs + // { + src = filterSrcWithRegexes dep-files-filter ./.; + inherit pname; + }); + + # Compile and cache only workspace code (seperately from 3rc party dependencies) + package-file-filter = + dep-files-filter + ++ [ + ".*\.rs" + ]; + cargo-package = craneLib.buildPackage (commonArgs + // { + inherit cargo-deps pname; + src = filterSrcWithRegexes package-file-filter ./.; + }); + in { + checks = { + # inherit # Comment in when you want tests to run on every new shell + # cargo-package + # ; + }; + devShells.default = pkgs.mkShell { + packages = + (with pkgs; [ + # rust specific + cargo-audit + cargo-auditable + cargo-cross + cargo-deny + cargo-outdated + rust-analyzer + + # Editor stuffs + helix + lldb + rust-analyzer + + # Nix stuff + nix-output-monitor + ]) + ++ commonArgs.nativeBuildInputs + ++ [ + # Packages made in this flake + rustToolchain + # cargo-package # Comment in when you want tests to run on every new shell + ] + ++ lib.optionals (pkgs.stdenv.isLinux) (with pkgs; [cargo-watch]); # Currently broken on macOS + + shellHook = '' + ${rustToolchain}/bin/cargo --version + ${pkgs.helix}/bin/hx --version + ${pkgs.helix}/bin/hx --health rust + ''; + }; + packages = { + rust = cargo-package; + docker = pkgs.dockerTools.buildImage { + name = pname; + tag = "v${cargo-details.package.version}"; + extraCommands = ''mkdir -p data''; + config = { + Cmd = "--help"; + Entrypoint = ["${cargo-package}/bin/${pname}"]; + }; + }; + }; + packages.default = cargo-package; + + # Now `nix fmt` works! + formatter = pkgs.nixfmt; + }); +}