From 18b8a859ea070547c7099810dc3041c7d364d9e0 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Wed, 5 Mar 2025 14:39:13 +0100 Subject: [PATCH 1/4] Create nix flake --- .envrc | 1 + .gitignore | 6 +++ flake.lock | 85 ++++++++++++++++++++++++++++++++++ flake.nix | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++ treefmt.nix | 8 ++++ 5 files changed, 230 insertions(+) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 treefmt.nix diff --git a/.envrc b/.envrc new file mode 100644 index 00000000000..8392d159f2e --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake \ No newline at end of file diff --git a/.gitignore b/.gitignore index 689a13a3103..77b0a869801 100644 --- a/.gitignore +++ b/.gitignore @@ -213,3 +213,9 @@ new.json # Keys *.pem .ok.sql + +# direnv/envrc cache +.direnv + +# Nix build output +result \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000000..5aee7329d69 --- /dev/null +++ b/flake.lock @@ -0,0 +1,85 @@ +{ + "nodes": { + "crane": { + "locked": { + "lastModified": 1741148495, + "narHash": "sha256-EV8KUaIZ2/CdBXlutXrHoZYbWPeB65p5kKZk71gvDRI=", + "owner": "ipetkov", + "repo": "crane", + "rev": "75390a36cd0c2cdd5f1aafd8a9f827d7107f2e53", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1741037377, + "narHash": "sha256-SvtvVKHaUX4Owb+PasySwZsoc5VUeTf1px34BByiOxw=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "02032da4af073d0f6110540c8677f16d4be0117f", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay", + "treefmt-nix": "treefmt-nix" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1741141853, + "narHash": "sha256-FauVtC+FbOgkKpGVuQTNxSqrvgbmVc7hFkjn/DacwMo=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "02edad1f19d6dec824e0812e4cdc0aa7930ff8ae", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1739829690, + "narHash": "sha256-mL1szCeIsjh6Khn3nH2cYtwO5YXG6gBiTw1A30iGeDU=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "3d0579f5cc93436052d94b73925b48973a104204", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000000..84d5022ca12 --- /dev/null +++ b/flake.nix @@ -0,0 +1,130 @@ +{ + description = "A Nix-flake-based Rust development environment"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + + crane.url = "github:ipetkov/crane"; + + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = + { + self, + nixpkgs, + crane, + treefmt-nix, + rust-overlay, + }: + let + supportedSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + forEachSupportedSystem = + f: + nixpkgs.lib.genAttrs supportedSystems ( + system: + f (rec { + pkgs = import nixpkgs { + inherit system; + overlays = [ (import rust-overlay) ]; + }; + + rustToolchainFor = pkgs: pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + rustToolchain = rustToolchainFor pkgs; + + # NB: we don't need to overlay our custom toolchain for the *entire* + # pkgs (which would require rebuidling anything else which uses rust). + # Instead, we just want to update the scope that crane will use by appending + # our specific toolchain there. + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchainFor; + }) + ); + in + { + formatter = forEachSupportedSystem ( + { pkgs, ... }: (treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.wrapper + ); + devShells = forEachSupportedSystem ( + { pkgs, rustToolchain, ... }: + { + default = pkgs.mkShell { + packages = [ + rustToolchain + ]; + }; + } + ); + packages = forEachSupportedSystem ( + { + pkgs, + rustToolchain, + craneLib, + ... + }: + let + rustToolchainSettings = (pkgs.lib.importTOML ./rust-toolchain.toml).toolchain; + workspaceManifest = (pkgs.lib.importTOML ./Cargo.toml).workspace.package; + + # Build a workspace member, specified by its Cargo.toml + buildPackage = + manifestFile: nativeBuildInputs: overrides: + ( + let + manifest = (pkgs.lib.importTOML manifestFile).package; + in + craneLib.buildPackage ( + { + # Only build the specified package + cargoExtraArgs = "-p " + manifest.name; + + pname = manifest.name; + src = ./.; + version = workspaceManifest.version; + strictDeps = true; + + inherit nativeBuildInputs; + } + // overrides + ) + ); + in + { + spacetimedb-cli = buildPackage (./crates/cli/Cargo.toml) (with pkgs; [ + git + perl + ]) ({ }); + spacetimedb-standalone = buildPackage (./crates/standalone/Cargo.toml) (with pkgs; [ perl ]) ({ }); + spacetimedb-update = buildPackage (./crates/update/Cargo.toml) (with pkgs; [ pkg-config ]) ({ + env.OPENSSL_DIR = "${pkgs.openssl.dev}"; + env.OPENSSL_LIB_DIR = "${pkgs.lib.getLib pkgs.openssl}/lib"; + env.OPENSSL_NO_VENDOR = 1; + }); + } + ); + + overlays.default = final: prev: { + spacetimedb-cli = self.packages.spacetimedb-cli.${final.system}; + spacetimedb-standalone = self.packages.spacetimedb-standalone.${final.system}; + spacetimedb-update = self.packages.spacetimedb-update.${final.system}; + }; + + nixosModules.spacetimedb = + { pkgs, ... }: + { + nixpkgs.overlays = [ self.overlay ]; + }; + }; +} diff --git a/treefmt.nix b/treefmt.nix new file mode 100644 index 00000000000..994bebd4e5d --- /dev/null +++ b/treefmt.nix @@ -0,0 +1,8 @@ +{ ... }: +{ + projectRootFile = "flake.nix"; + programs = { + nixfmt.enable = true; + rustfmt.enable = true; + }; +} From 4ce3e8828961fcb064ec6572446307273fb06a8e Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Wed, 5 Mar 2025 14:47:03 +0100 Subject: [PATCH 2/4] Run nix fmt which for some reason catches this where regular `cargo fmt` misses it --- crates/bindings/tests/ui/tables.rs | 5 ++++- crates/bindings/tests/ui/tables.stderr | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/bindings/tests/ui/tables.rs b/crates/bindings/tests/ui/tables.rs index 41c1268bf2d..4157457d655 100644 --- a/crates/bindings/tests/ui/tables.rs +++ b/crates/bindings/tests/ui/tables.rs @@ -14,7 +14,10 @@ struct TypeParam { struct ConstParam {} #[derive(spacetimedb::SpacetimeType)] -enum Alpha { Beta, Gamma } +enum Alpha { + Beta, + Gamma, +} #[spacetimedb::table(name = delta)] struct Delta { diff --git a/crates/bindings/tests/ui/tables.stderr b/crates/bindings/tests/ui/tables.stderr index b93b3a7e2d3..ba033ec0cdf 100644 --- a/crates/bindings/tests/ui/tables.stderr +++ b/crates/bindings/tests/ui/tables.stderr @@ -118,9 +118,9 @@ note: required by a bound in `spacetimedb::spacetimedb_lib::ser::SerializeNamedP | ^^^^^^^^^ required by this bound in `SerializeNamedProduct::serialize_element` error[E0277]: `&'a Alpha` cannot appear as an argument to an index filtering operation - --> tests/ui/tables.rs:30:33 + --> tests/ui/tables.rs:33:33 | -30 | ctx.db.delta().compound_a().find(Alpha::Beta); +33 | ctx.db.delta().compound_a().find(Alpha::Beta); | ^^^^ should be an integer type, `bool`, `String`, `&str`, `Identity`, `ConnectionId`, or `Hash`, not `&'a Alpha` | = help: the trait `for<'a> FilterableValue` is not implemented for `&'a Alpha` @@ -145,9 +145,9 @@ note: required by a bound in `UniqueColumn::::ColType, Col>::find` error[E0277]: the trait bound `Alpha: IndexScanRangeBounds<(Alpha,), SingleBound>` is not satisfied - --> tests/ui/tables.rs:31:40 + --> tests/ui/tables.rs:34:40 | -31 | ctx.db.delta().compound_b().filter(Alpha::Gamma); +34 | ctx.db.delta().compound_b().filter(Alpha::Gamma); | ------ ^^^^^^^^^^^^ the trait `FilterableValue` is not implemented for `Alpha` | | | required by a bound introduced by this call From 629180a9d1b69df5136cca25c75af480ad07c668 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Wed, 5 Mar 2025 15:03:00 +0100 Subject: [PATCH 3/4] Tidy up --- flake.nix | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index 84d5022ca12..d751446708d 100644 --- a/flake.nix +++ b/flake.nix @@ -75,7 +75,6 @@ ... }: let - rustToolchainSettings = (pkgs.lib.importTOML ./rust-toolchain.toml).toolchain; workspaceManifest = (pkgs.lib.importTOML ./Cargo.toml).workspace.package; # Build a workspace member, specified by its Cargo.toml @@ -116,9 +115,9 @@ ); overlays.default = final: prev: { - spacetimedb-cli = self.packages.spacetimedb-cli.${final.system}; - spacetimedb-standalone = self.packages.spacetimedb-standalone.${final.system}; - spacetimedb-update = self.packages.spacetimedb-update.${final.system}; + spacetimedb-cli = self.packages.${final.system}.spacetimedb-cli; + spacetimedb-standalone = self.packages.${final.system}.spacetimedb-standalone; + spacetimedb-update = self.packages.${final.system}.spacetimedb-update; }; nixosModules.spacetimedb = From 247127efd92c54550503bcad39842ba258aaaf21 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Thu, 6 Mar 2025 12:29:31 +0100 Subject: [PATCH 4/4] Suggested improvements --- .gitignore | 2 +- flake.nix | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 77b0a869801..81d2d200c57 100644 --- a/.gitignore +++ b/.gitignore @@ -218,4 +218,4 @@ new.json .direnv # Nix build output -result \ No newline at end of file +result* diff --git a/flake.nix b/flake.nix index d751446708d..5a7a0be8693 100644 --- a/flake.nix +++ b/flake.nix @@ -104,13 +104,13 @@ spacetimedb-cli = buildPackage (./crates/cli/Cargo.toml) (with pkgs; [ git perl - ]) ({ }); - spacetimedb-standalone = buildPackage (./crates/standalone/Cargo.toml) (with pkgs; [ perl ]) ({ }); - spacetimedb-update = buildPackage (./crates/update/Cargo.toml) (with pkgs; [ pkg-config ]) ({ + ]) { }; + spacetimedb-standalone = buildPackage (./crates/standalone/Cargo.toml) (with pkgs; [ perl ]) { }; + spacetimedb-update = buildPackage (./crates/update/Cargo.toml) (with pkgs; [ pkg-config ]) { env.OPENSSL_DIR = "${pkgs.openssl.dev}"; env.OPENSSL_LIB_DIR = "${pkgs.lib.getLib pkgs.openssl}/lib"; env.OPENSSL_NO_VENDOR = 1; - }); + }; } );