Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Nix Flake #2345

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,9 @@ new.json
# Keys
*.pem
.ok.sql

# direnv/envrc cache
.direnv

# Nix build output
result*
5 changes: 4 additions & 1 deletion crates/bindings/tests/ui/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ struct TypeParam<T> {
struct ConstParam<const X: u8> {}

#[derive(spacetimedb::SpacetimeType)]
enum Alpha { Beta, Gamma }
enum Alpha {
Beta,
Gamma,
}

#[spacetimedb::table(name = delta)]
struct Delta {
Expand Down
8 changes: 4 additions & 4 deletions crates/bindings/tests/ui/tables.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -145,9 +145,9 @@ note: required by a bound in `UniqueColumn::<Tbl, <Col as spacetimedb::table::Co
| ^^^^^^^^^^^^^^^ required by this bound in `UniqueColumn::<Tbl, <Col as Column>::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
Expand Down
85 changes: 85 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 129 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
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
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.${final.system}.spacetimedb-cli;
spacetimedb-standalone = self.packages.${final.system}.spacetimedb-standalone;
spacetimedb-update = self.packages.${final.system}.spacetimedb-update;
};

nixosModules.spacetimedb =
{ pkgs, ... }:
{
nixpkgs.overlays = [ self.overlay ];
};
};
}
8 changes: 8 additions & 0 deletions treefmt.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ ... }:
{
projectRootFile = "flake.nix";
programs = {
nixfmt.enable = true;
rustfmt.enable = true;
};
}