Skip to content

Commit

Permalink
Merge pull request #1468 from cachix/feat-nix-caching
Browse files Browse the repository at this point in the history
feat: cache nix commands
  • Loading branch information
domenkozar authored Oct 2, 2024
2 parents eb86c60 + 5a32581 commit 82c0147
Show file tree
Hide file tree
Showing 30 changed files with 3,058 additions and 222 deletions.
973 changes: 944 additions & 29 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["devenv", "devenv-run-tests", "xtask", "tasks"]
members = ["devenv", "devenv-eval-cache", "devenv-run-tests", "tasks", "xtask"]

[workspace.package]
edition = "2021"
Expand All @@ -9,14 +9,23 @@ homepage = "https://devenv.sh/"
repository = "https://github.com/cachix/devenv/"

[workspace.dependencies]
devenv = { path = "devenv" }
devenv-eval-cache = { path = "devenv-eval-cache" }
devenv-run-tests = { path = "devenv-run-tests" }
tasks = { path = "tasks" }
xtask = { path = "xtask" }

ansiterm = "0.12.2"
blake3 = "1.5.4"
clap = { version = "4.5.1", features = ["derive", "cargo"] }
cli-table = "0.4.7"
dotlock = "0.5.0"
fs2 = "0.4.3"
futures = "0.3.30"
hex = "0.4.3"
include_dir = "0.7.3"
indoc = "2.0.4"
lazy_static = "1.5.0"
miette = { version = "7.1.0", features = ["fancy"] }
nix = { version = "0.28.0", features = ["signal"] }
regex = "1.10.3"
Expand All @@ -29,9 +38,12 @@ schematic = { version = "0.14.3", features = [
] }
serde = "1.0.197"
serde_json = "1.0.114"
serde_repr = "0.1.19"
serde_yaml = "0.9.32"
sha2 = "0.10.8"
sqlx = { version = "0.8.2", features = ["time", "sqlite", "runtime-tokio"] }
tempdir = "0.3.7"
thiserror = "1.0.63"
tracing = "0.1.40"
which = "6.0.0"
whoami = "1.5.1"
Expand All @@ -43,3 +55,11 @@ tokio = { version = "1.39.3", features = [
"rt-multi-thread",
] }
schemars = "0.8.16"

# Always build optimized sqlx-macro to speed up query checks
[profile.dev.package.sqlx-macros]
opt-level = 3

[profile.release]
strip = true
lto = "fat"
22 changes: 22 additions & 0 deletions devenv-eval-cache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "devenv-eval-cache"
# TODO: switch to workspace version
version = "0.1.0"
edition.workspace = true
license.workspace = true

[dependencies]
blake3.workspace = true
futures.workspace = true
lazy_static.workspace = true
miette.workspace = true
regex.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_repr.workspace = true
sqlx.workspace = true
thiserror.workspace = true
tokio.workspace = true

[dev-dependencies]
tempdir.workspace = true
43 changes: 43 additions & 0 deletions devenv-eval-cache/migrations/20240906130404_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE TABLE IF NOT EXISTS cached_cmd
(
id INTEGER NOT NULL PRIMARY KEY,
raw TEXT NOT NULL,
cmd_hash CHAR(64) NOT NULL UNIQUE,
input_hash CHAR(64) NOT NULL,
output TEXT NOT NULL,
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))
);

CREATE INDEX IF NOT EXISTS idx_cached_cmd_hash ON cached_cmd(cmd_hash);

CREATE TABLE IF NOT EXISTS file_path
(
id INTEGER NOT NULL PRIMARY KEY,
path BLOB NOT NULL UNIQUE,
is_directory BOOLEAN NOT NULL,
content_hash CHAR(64) NOT NULL,
modified_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))
);

CREATE INDEX IF NOT EXISTS idx_file_path ON file_path(path);

CREATE TABLE IF NOT EXISTS cmd_input_path
(
id INTEGER NOT NULL PRIMARY KEY,
cached_cmd_id INTEGER,
file_path_id INTEGER,
UNIQUE(cached_cmd_id, file_path_id),
FOREIGN KEY(cached_cmd_id)
REFERENCES cached_cmd(id)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY(file_path_id)
REFERENCES file_path(id)
ON UPDATE CASCADE
ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_cmd_input_path_cached_cmd_id ON cmd_input_path(cached_cmd_id);
CREATE INDEX IF NOT EXISTS idx_cmd_input_path_file_path_id ON cmd_input_path(file_path_id);
CREATE INDEX IF NOT EXISTS idx_cmd_input_path_composite ON cmd_input_path(cached_cmd_id, file_path_id);
17 changes: 17 additions & 0 deletions devenv-eval-cache/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::process::Command;

use devenv_eval_cache::{command, db};

#[tokio::main]
async fn main() -> Result<(), command::CommandError> {
let database_url = "sqlite:nix-eval-cache.db";
let pool = db::setup_db(database_url).await?;

let mut cmd = Command::new("nix");
cmd.args(["eval", ".#devenv.processes"]);

let output = command::CachedCommand::new(&pool).output(&mut cmd).await?;
println!("{}", String::from_utf8_lossy(&output.stdout));

Ok(())
}
Loading

0 comments on commit 82c0147

Please sign in to comment.