Skip to content

Commit

Permalink
Merge branch 'main' into revamp-frontpage
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar authored Apr 12, 2024
2 parents 452b7bd + f69a72d commit d11c55e
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 42 deletions.
73 changes: 73 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions devenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ clap = { version = "4.5.1", features = ["derive", "cargo"] }
cli-table = "0.4.7"
dotlock = "0.5.0"
fs2 = "0.4.3"
hex = "0.4.3"
include_dir = "0.7.3"
indoc = "2.0.4"
miette = { version = "7.1.0", features = ["fancy"] }
Expand All @@ -20,6 +21,7 @@ schematic = { version = "0.14.3", features = ["schema", "yaml", "renderer_templa
serde = "1.0.197"
serde_json = "1.0.114"
serde_yaml = "0.9.32"
sha2 = "0.10.8"
tempdir = "0.3.7"
tracing = "0.1.40"
which = "6.0.0"
Expand Down
3 changes: 2 additions & 1 deletion devenv/src/flake.tmpl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
devenv.dotfile = devenv_root + "/" + devenv_dotfile_string;
}
(pkgs.lib.optionalAttrs (inputs.devenv.isTmpDir or false) {
devenv.tmpdir = tmpdir;
devenv.tmpdir = devenv_tmpdir;
devenv.runtime = devenv_runtime;
})
(pkgs.lib.optionalAttrs (container_name != null) {
container.isBuilding = pkgs.lib.mkForce true;
Expand Down
25 changes: 19 additions & 6 deletions devenv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cli_table::{print_stderr, Table, WithTitle};
use include_dir::{include_dir, Dir};
use miette::{bail, Result};
use serde::Deserialize;
use sha2::Digest;
use std::collections::HashMap;
use std::io::Write;
use std::os::unix::fs::symlink;
Expand Down Expand Up @@ -246,6 +247,8 @@ struct App {
devenv_dotfile: PathBuf,
devenv_dot_gc: PathBuf,
devenv_home_gc: PathBuf,
devenv_tmp: String,
devenv_runtime: PathBuf,
cachix_trusted_keys: PathBuf,
cachix_caches: Option<command::CachixCaches>,
}
Expand All @@ -270,6 +273,16 @@ fn main() -> Result<()> {
let devenv_dot_gc = devenv_root.join(".devenv").join("gc");
std::fs::create_dir_all(&devenv_dot_gc).expect("Failed to create .devenv/gc directory");
let devenv_dotfile = devenv_root.join(".devenv");
let devenv_tmp = std::env::var("XDG_RUNTIME_DIR")
.unwrap_or_else(|_| std::env::var("TMPDIR").unwrap_or_else(|_| "/tmp".to_string()));
// first 7 chars of sha256 hash of devenv_state
let devenv_state_hash = {
let mut hasher = sha2::Sha256::new();
hasher.update(devenv_dotfile.to_string_lossy().as_bytes());
let result = hasher.finalize();
hex::encode(result)
};
let devenv_runtime = Path::new(&devenv_tmp).join(format!("devenv-{}", &devenv_state_hash[..7]));
let cachix_trusted_keys = devenv_home.join("cachix_trusted_keys.json");
let logger = log::Logger::new(level);
let mut config = config::Config::load()?;
Expand All @@ -286,6 +299,8 @@ fn main() -> Result<()> {
devenv_dotfile,
devenv_dot_gc,
devenv_home_gc,
devenv_tmp,
devenv_runtime,
cachix_trusted_keys,
cachix_caches: None,
};
Expand Down Expand Up @@ -821,9 +836,6 @@ impl App {
.to_string();
if test_script_string.is_empty() {
self.logger.error("No tests found.");
tmpdir
.close()
.expect("Failed to remove temporary directory");
bail!("No tests found");
}

Expand Down Expand Up @@ -1076,7 +1088,8 @@ impl App {
devenv_dotfile = ./{};
devenv_dotfile_string = \"{}\";
container_name = {};
tmpdir = \"{}\";
devenv_tmpdir = \"{}\";
devenv_runtime = \"{}\";
",
crate_version!(),
self.cli.system,
Expand All @@ -1087,8 +1100,8 @@ impl App {
.as_deref()
.map(|s| format!("\"{}\"", s))
.unwrap_or_else(|| "null".to_string()),
std::env::var("XDG_RUNTIME_DIR")
.unwrap_or_else(|_| std::env::var("TMPDIR").unwrap_or_else(|_| "/tmp".to_string())),
self.devenv_tmp,
self.devenv_runtime.display(),
);
let flake = FLAKE_TMPL.replace("__DEVENV_VARS__", &vars);
std::fs::write(DEVENV_FLAKE, flake).expect("Failed to write flake.nix");
Expand Down
24 changes: 12 additions & 12 deletions flake.lock

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

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
});

modules = ./src/modules;
hasTmpDir = true;
isTmpDir = true;

templates =
let
Expand Down
2 changes: 1 addition & 1 deletion src/modules/flake.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
outputs = { ... }: {
modules = ./.;
hasTmpDir = true;
isTmpDir = true;
};
}
1 change: 1 addition & 0 deletions src/modules/processes.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ in
wait $backgroundPID
${config.process.after}
echo "Processes stopped."
rm -rf ${config.devenv.runtime}
}
trap stop_up SIGINT SIGTERM
Expand Down
39 changes: 18 additions & 21 deletions src/modules/top-level.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ let
${lib.concatStringsSep "\n" (builtins.map formatAssertionMessage failedAssertions)}
''
else lib.trivial.showWarnings config.warnings;

q = lib.escapeShellArg;
in
{
options = {
Expand Down Expand Up @@ -169,6 +167,19 @@ in
runtime = lib.mkOption {
type = types.str;
internal = true;
# The path has to be
# - unique to each DEVENV_STATE to let multiple devenv environments coexist
# - deterministic so that it won't change constantly
# - short so that unix domain sockets won't hit the path length limit
# - free to create as an unprivileged user across OSes
default =
let
hashedRoot = builtins.hashString "sha256" config.devenv.state;

# same length as git's abbreviated commit hashes
shortHash = builtins.substring 0 7 hashedRoot;
in
"${config.devenv.tmpdir}/devenv-${shortHash}";
};

tmpdir = lib.mkOption {
Expand Down Expand Up @@ -223,20 +234,6 @@ in
devenv.dotfile = lib.mkDefault (builtins.toPath (config.devenv.root + "/.devenv"));
devenv.profile = profile;

# The path has to be
# - unique to each DEVENV_STATE to let multiple devenv environments coexist
# - deterministic so that it won't change constantly
# - short so that unix domain sockets won't hit the path length limit
# - free to create as an unprivileged user across OSes
devenv.runtime =
let
hashedRoot = builtins.hashString "sha256" config.devenv.state;

# same length as git's abbreviated commit hashes
shortHash = builtins.substring 0 7 hashedRoot;
in
"${config.devenv.tmpdir}/devenv-${shortHash}";

env.DEVENV_PROFILE = config.devenv.profile;
env.DEVENV_STATE = config.devenv.state;
env.DEVENV_RUNTIME = config.devenv.runtime;
Expand Down Expand Up @@ -269,15 +266,15 @@ in
echo "Please install direnv: https://direnv.net/docs/installation.html"
fi
mkdir -p $DEVENV_STATE
if [ ! -L .devenv/profile ] || [ "$(${pkgs.coreutils}/bin/readlink .devenv/profile)" != "${profile}" ]
mkdir -p "$DEVENV_STATE"
if [ ! -L "$DEVENV_DOTFILE/profile" ] || [ "$(${pkgs.coreutils}/bin/readlink $DEVENV_DOTFILE/profile)" != "${profile}" ]
then
ln -nsf ${profile} .devenv/profile
ln -nsf ${profile} "$DEVENV_DOTFILE/profile"
fi
unset ${lib.concatStringsSep " " config.unsetEnvVars}
mkdir -p ${q config.devenv.runtime}
ln -fs ${q config.devenv.runtime} ${q config.devenv.dotfile}/run
mkdir -p ${lib.escapeShellArg config.devenv.runtime}
ln -fs ${lib.escapeShellArg config.devenv.runtime} ${lib.escapeShellArg config.devenv.dotfile}/run
'';

shell = performAssertions (
Expand Down
1 change: 1 addition & 0 deletions tests/unversioned-devenv-nix/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
devenv.nix
1 change: 1 addition & 0 deletions tests/unversioned-devenv-nix/.setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "{ env.FOO = "1"; }" > devenv.nix
1 change: 1 addition & 0 deletions tests/unversioned-devenv-nix/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo $FOO | grep -q 1

0 comments on commit d11c55e

Please sign in to comment.