summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2022-12-27 19:56:00 -0500
committerKjetil Orbekk <kj@orbekk.com>2022-12-27 19:58:45 -0500
commit4c474b2ee6716d5c067ae20903e374073cefaa3d (patch)
tree9be4d721eda7417497e2187cccbaddf8b274e6ac
parent21b7b57336cd3bf7bd328ada38c6069a48504f85 (diff)
Create nix packages for project
-rw-r--r--Cargo.lock5
-rw-r--r--flake.lock130
-rw-r--r--flake.nix171
-rw-r--r--server/Cargo.toml2
-rw-r--r--webapp/Trunk.toml2
5 files changed, 225 insertions, 85 deletions
diff --git a/Cargo.lock b/Cargo.lock
index afac954..04acb88 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -449,6 +449,9 @@ name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
+dependencies = [
+ "serde",
+]
[[package]]
name = "encoding_rs"
@@ -2104,9 +2107,11 @@ dependencies = [
"dotenvy",
"either",
"heck",
+ "hex",
"once_cell",
"proc-macro2",
"quote",
+ "serde",
"serde_json",
"sha2",
"sqlx-core",
diff --git a/flake.lock b/flake.lock
index 4fb607b..67b59bc 100644
--- a/flake.lock
+++ b/flake.lock
@@ -1,12 +1,66 @@
{
"nodes": {
+ "crane": {
+ "inputs": {
+ "flake-compat": "flake-compat",
+ "flake-utils": "flake-utils",
+ "nixpkgs": [
+ "nixpkgs"
+ ],
+ "rust-overlay": "rust-overlay"
+ },
+ "locked": {
+ "lastModified": 1672095661,
+ "narHash": "sha256-7NTsdCn3qsvU7A+1/7tY8pxbq0DYy1pFYNpzN6he9lI=",
+ "owner": "ipetkov",
+ "repo": "crane",
+ "rev": "98894bb39b03bfb379c5e10523cd61160e1ac782",
+ "type": "github"
+ },
+ "original": {
+ "owner": "ipetkov",
+ "repo": "crane",
+ "type": "github"
+ }
+ },
+ "flake-compat": {
+ "flake": false,
+ "locked": {
+ "lastModified": 1668681692,
+ "narHash": "sha256-Ht91NGdewz8IQLtWZ9LCeNXMSXHUss+9COoqu6JLmXU=",
+ "owner": "edolstra",
+ "repo": "flake-compat",
+ "rev": "009399224d5e398d03b22badca40a37ac85412a1",
+ "type": "github"
+ },
+ "original": {
+ "owner": "edolstra",
+ "repo": "flake-compat",
+ "type": "github"
+ }
+ },
"flake-utils": {
"locked": {
- "lastModified": 1659877975,
- "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
+ "lastModified": 1667395993,
+ "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
- "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
+ "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
+ "type": "github"
+ },
+ "original": {
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "type": "github"
+ }
+ },
+ "flake-utils_2": {
+ "locked": {
+ "lastModified": 1667395993,
+ "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
@@ -17,20 +71,74 @@
},
"nixpkgs": {
"locked": {
- "lastModified": 0,
- "narHash": "sha256-Kqd6r9aNIzjYGhMFYATdKwRbXBCGLcdqAJMLr4I8LG0=",
- "path": "/nix/store/l0zpkxzcinla9fr0rf9qy18gga43fxml-source",
- "type": "path"
+ "lastModified": 1672158941,
+ "narHash": "sha256-A6KENZ8/ke7YXpmpwVH4nTf5hLreFEUWW9Q+Z9jdomg=",
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "23d58dae80cb6a9e14839be4e626ec4bc0963e32",
+ "type": "github"
},
"original": {
- "id": "nixpkgs",
- "type": "indirect"
+ "owner": "NixOS",
+ "ref": "nixpkgs-unstable",
+ "repo": "nixpkgs",
+ "type": "github"
}
},
"root": {
"inputs": {
- "flake-utils": "flake-utils",
- "nixpkgs": "nixpkgs"
+ "crane": "crane",
+ "flake-utils": "flake-utils_2",
+ "nixpkgs": "nixpkgs",
+ "rust-overlay": "rust-overlay_2"
+ }
+ },
+ "rust-overlay": {
+ "inputs": {
+ "flake-utils": [
+ "crane",
+ "flake-utils"
+ ],
+ "nixpkgs": [
+ "crane",
+ "nixpkgs"
+ ]
+ },
+ "locked": {
+ "lastModified": 1670034122,
+ "narHash": "sha256-EqmuOKucPWtMvCZtHraHr3Q3bgVszq1x2PoZtQkUuEk=",
+ "owner": "oxalica",
+ "repo": "rust-overlay",
+ "rev": "a0d5773275ecd4f141d792d3a0376277c0fc0b65",
+ "type": "github"
+ },
+ "original": {
+ "owner": "oxalica",
+ "repo": "rust-overlay",
+ "type": "github"
+ }
+ },
+ "rust-overlay_2": {
+ "inputs": {
+ "flake-utils": [
+ "flake-utils"
+ ],
+ "nixpkgs": [
+ "nixpkgs"
+ ]
+ },
+ "locked": {
+ "lastModified": 1672107670,
+ "narHash": "sha256-m4kP+8k46JwSXYDugykIVvRyoNofZDG7atjbi5+sLoU=",
+ "owner": "oxalica",
+ "repo": "rust-overlay",
+ "rev": "f4827ef0518463f31a52ab2e5c500c80558fdd78",
+ "type": "github"
+ },
+ "original": {
+ "owner": "oxalica",
+ "repo": "rust-overlay",
+ "type": "github"
}
}
},
diff --git a/flake.nix b/flake.nix
index edba281..a2aa32d 100644
--- a/flake.nix
+++ b/flake.nix
@@ -1,81 +1,108 @@
{
inputs = {
+ nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
+ crane.url = "github:ipetkov/crane";
+ crane.inputs.nixpkgs.follows = "nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
- nixpkgs.url = "nixpkgs";
+ rust-overlay = {
+ url = "github:oxalica/rust-overlay";
+ inputs = {
+ nixpkgs.follows = "nixpkgs";
+ flake-utils.follows = "flake-utils";
+ };
+ };
};
- # TODO: Stuff to include in rust development environment:
- # - trunk
- # - cargo-watch
- # - sqlx-cli
- outputs = { self, nixpkgs, flake-utils }:
- flake-utils.lib.eachDefaultSystem (system:
- let pkgs = nixpkgs.legacyPackages.${system};
- in {
- packages.databaseImage = pkgs.dockerTools.buildImage {
- name = "bridge-database";
- contents = [ pkgs.shadow pkgs.postgresql_12 pkgs.bashInteractive pkgs.busybox ];
- tag = "latest";
+ outputs = { self, nixpkgs, crane, flake-utils, rust-overlay, ... }:
+ let
+ systems = with flake-utils.lib.system; [x86_64-linux x86_64-darwin aarch64-linux aarch64-darwin];
+ in
+ flake-utils.lib.eachSystem systems (system:
+ let
+ pkgs = import nixpkgs {
+ inherit system;
+ overlays = [ (import rust-overlay) ];
+ };
- runAsRoot = ''
- #!${pkgs.stdenv.shell}
- ${pkgs.dockerTools.shadowSetup}
- useradd postgres
- mkdir -p /var/lib/postgres
- chown postgres /var/lib/postgres
- su postgres -c 'initdb -D /var/lib/postgres/data'
- '';
+ craneLib = crane.lib.${system};
- config = {
- Cmd = [ "/bin/su" "postgres" "-c" "/bin/postgres -D /var/lib/postgres/data" ];
- };
- };
+ buildInputs = with pkgs; [
+ trunk
+ cargo-wasi
+ openssl
+ pkg-config
+ wasm-bindgen-cli
+ ];
- devShell = pkgs.mkShell {
- buildInputs = with pkgs; [
- podman-compose
- stdenv
- postgresql
- openssl
- pkg-config
- llvmPackages_latest.llvm
- llvmPackages_latest.bintools
- zlib.out
- rustup
- rust-analyzer
- #xorriso
- #grub2
- #qemu
- llvmPackages_latest.lld
- #python3
- ];
- RUSTC_VERSION = pkgs.lib.readFile ./rust-toolchain;
- # https://github.com/rust-lang/rust-bindgen#environment-variables
- LIBCLANG_PATH =
- pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ];
- HISTFILE = toString ./.history;
- shellHook = ''
- export PATH=$PATH:~/.cargo/bin
- export PATH=$PATH:~/.rustup/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/
- '';
- # Add libvmi precompiled library to rustc search path
- # RUSTFLAGS = (builtins.map (a: ''-L ${a}/lib'') [
- # pkgs.libvmi
- # ]);
- # Add libvmi, glibc, clang, glib headers to bindgen search path
- BINDGEN_EXTRA_CLANG_ARGS =
- # Includes with normal include path
- (builtins.map (a: ''-I"${a}/include"'') [
- # pkgs.libvmi
- pkgs.glibc.dev
- ])
- # Includes with special directory paths
- ++ [
- ''
- -I"${pkgs.llvmPackages_latest.libclang.lib}/lib/clang/${pkgs.llvmPackages_latest.libclang.version}/include"''
- ''-I"${pkgs.glib.dev}/include/glib-2.0"''
- "-I${pkgs.glib.out}/lib/glib-2.0/include/"
- ];
- };
- });
+ rustWithWasmTarget = pkgs.rust-bin.stable.latest.default.override {
+ targets = [ "wasm32-unknown-unknown" "wasm32-wasi" ];
+ };
+
+ craneLibWithWasmTarget = (crane.mkLib pkgs).overrideToolchain rustWithWasmTarget;
+
+ commonArgs = {
+ inherit buildInputs;
+ src = craneLib.cleanCargoSource ./.;
+ };
+
+ commonArgsForWasm = commonArgs // {
+ doCheck = false;
+ };
+
+ cargoArtifactsForServer = craneLib.buildDepsOnly (commonArgs // {
+ cargoExtraArgs = "--workspace --exclude webapp";
+ });
+
+ cargoArtifactsForWasm = craneLibWithWasmTarget.buildDepsOnly (commonArgsForWasm // {
+ cargoExtraArgs = "--workspace --exclude server --target wasm32-unknown-unknown";
+ });
+
+ in
+ {
+ # TODO add checks:
+ # - cargo sqlx prepare --merged --check
+ checks.sqlx-prepare = craneLib.mkCargoDerivation (commonArgs // {
+ cargoArtifacts = cargoArtifactsForServer;
+ buildInputs = commonArgs.buildInputs ++ [ pkgs.sqlx-cli ];
+ buildPhaseCargoCommand = "cargo sqlx prepare --merged --check";
+ });
+
+ checks.fmt = craneLib.cargoFmt {
+ inherit (commonArgs) src;
+ };
+ packages.bridge-server = craneLib.buildPackage (commonArgs // {
+ src = ./.;
+ });
+
+ packages.bridge-webapp = craneLibWithWasmTarget.buildPackage (commonArgsForWasm // {
+ cargoArtifacts = cargoArtifactsForWasm;
+ src = ./.;
+ cargoExtraArgs = "--target wasm32-unknown-unknown";
+ buildPhaseCargoCommand = ''
+ pushd webapp
+ echo Running trunk from directory $PWD
+ trunk build
+ popd
+ '';
+ installPhaseCommand = ''
+ mkdir -p $out
+ cp -r webapp/dist/* $out
+ '';
+ });
+
+ devShells.default = pkgs.mkShell {
+ inputsFrom = builtins.attrValues self.packages;
+ nativeBuildInputs = with pkgs; [
+ cargo
+ cargo-watch
+ rustfmt
+ sqlx-cli
+ podman-compose
+ postgresql
+ rust-analyzer
+ pkg-config
+ openssl
+ ];
+ };
+ });
}
diff --git a/server/Cargo.toml b/server/Cargo.toml
index 2d7e866..9062058 100644
--- a/server/Cargo.toml
+++ b/server/Cargo.toml
@@ -21,7 +21,7 @@ uuid = { version = "1.1.2", features = ["serde", "fast-rng", "v4"] }
tower-cookies = "0.8.0"
tower = { version = "0.4.13", features = ["full"] }
urlencoding = "2.1.2"
-sqlx = { version = "0.6", features = ["runtime-tokio-native-tls", "postgres", "uuid", "chrono", "json"] }
+sqlx = { version = "0.6", features = ["runtime-tokio-native-tls", "postgres", "uuid", "chrono", "json", "offline"] }
anyhow = "1.0.65"
chrono = { version = "0.4.22", features = ["serde"] }
thiserror = "1.0.37"
diff --git a/webapp/Trunk.toml b/webapp/Trunk.toml
index e053ef1..7f7fa18 100644
--- a/webapp/Trunk.toml
+++ b/webapp/Trunk.toml
@@ -8,4 +8,4 @@ port = 8080
watch = ["src"]
[[proxy]]
-backend = "http://localhost:8081/api/" \ No newline at end of file
+backend = "http://localhost:18081/api/" \ No newline at end of file