Skip to content

Commit

Permalink
register flake inputs as gc roots
Browse files Browse the repository at this point in the history
  • Loading branch information
TyberiusPrime committed Nov 18, 2024
1 parent f5ccf3e commit 0bdda1a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/flake_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@ fn copy_for_poetry(
}
}
}
//I'd love to return these relative, but since we run ancient-poetry in a tmp dir,
//this will fail.
Ok(target_path.canonicalize()?.to_string_lossy().to_string())
}

Expand Down
25 changes: 24 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ fn inner_main() -> Result<()> {
&mut out_non_spec_but_cached_values,
)?;

if flake_changed.flake_nix_changed && !use_generated_file_instead {
register_flake_inputs_as_gc_root(&flake_dir)?;
}

if out_non_spec_but_cached_values != in_non_spec_but_cached_values {
save_cached_values(&flake_dir, &out_non_spec_but_cached_values)?;
}
Expand Down Expand Up @@ -1407,6 +1411,9 @@ fn nix_build_flake(url: &str) -> Result<String> {
})
}

////register the used tools and the flake itself as gcroots
///our own flake is automatically gc rooted.
///and teh flake-inputs are handled when/if the flake changed
pub fn register_nix_gc_root(url: &str, flake_dir: impl AsRef<Path>) -> Result<()> {
debug!("registering gc root for {}", url);
//where we store this stuff
Expand All @@ -1416,13 +1423,14 @@ pub fn register_nix_gc_root(url: &str, flake_dir: impl AsRef<Path>) -> Result<()
let (without_hash, _) = url
.rsplit_once('#')
.context("GC_root url should contain #")?;
//first we store and hash the flake itself and record tha.
//first we store and hash the flake itself and record that.
let flake_symlink_here = gc_roots.join(without_hash.replace('/', "_"));
if !flake_symlink_here.exists() {
let store_path = prefetch_flake(without_hash)?;
register_gc_root(&store_path, &flake_symlink_here)?;
}

//then we record it's output
let build_symlink_here = gc_roots.join(url.replace('/', "_"));
if !build_symlink_here.exists() {
let store_path = nix_build_flake(url)?;
Expand All @@ -1431,6 +1439,21 @@ pub fn register_nix_gc_root(url: &str, flake_dir: impl AsRef<Path>) -> Result<()
Ok(())
}

fn register_flake_inputs_as_gc_root(flake_dir: impl AsRef<Path>) -> Result<()> {
//run nix build .#flake_inputs_for_gc_root wit han output dir
Command::new("nix")
.args([
"build",
".#flake_inputs_for_gc_root",
"-o",
".gcroot_for_flake_inputs",
])
.current_dir(flake_dir)
.status()
.context("retrieving content for flake input gc root from nix failed")?;
Ok(())
}

fn attach_to_previous_container(flake_dir: impl AsRef<Path>) -> Result<()> {
let mut available: Vec<_> = fs::read_dir(flake_dir.as_ref().join("dtach"))
.context("Could not find dtach socket directory")?
Expand Down
25 changes: 21 additions & 4 deletions src/nix/flake_template.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#%INPUT_DEFS%
};

outputs = {
outputs = flake_inputs @ {
self,
#%INPUTS%
}:
Expand All @@ -27,13 +27,30 @@
};
helpers = import ./functions.nix {inherit pkgs;};
in rec {
defaultPackage = (helpers.buildSymlinkImage _args).derivation;
oci_image = helpers.buildOCIimage _args;
packages = {
default = (helpers.buildSymlinkImage _args).derivation;
oci_image = helpers.buildOCIimage _args;
flake_inputs_for_gc_root = pkgs.stdenv.mkDerivation {
pname = "anysnake2-flake-inputs";
version = "0.1";
unpackPhase = ":";
buildPhase = let
str_inputs =
builtins.concatStringsSep "\n"
(map (key: "ln -s ${flake_inputs.${key}} ${key}") (builtins.attrNames flake_inputs));
in
''
mkdir $out -p
cd $out/
''
+ str_inputs;
};
};
devShell = pkgs.stdenv.mkDerivation {
name = "anysnake2-devshell";
shellHook =
''
export PATH=${defaultPackage}/rootfs/bin:$PATH;
export PATH=${packages.default}/rootfs/bin:$PATH;
if test -f "develop_python_path.bash"; then
source "develop_python_path.bash"
fi
Expand Down

0 comments on commit 0bdda1a

Please sign in to comment.