Skip to content
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
6 changes: 4 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ reqwest = { version = "0.12.3", default-features = false, features = ["rustls-tl
serde = { version = "1.0.164", features = ["derive"] }
serde_json = "1.0.97"
gix = { version = "0.83.0", features = ["async-network-client", "serde"] }
#tar = { version = "0.4.38", features = ["xattr"] }
tar = { git = "https://github.com/DeterminateSystems/tar-rs.git", branch = "force-mtime", features = ["xattr"] }
tar = { version = "0.4.46", features = ["xattr"] }
flate2 = "1.0.26"
tempfile = "3.6.0"
ring = "0.17.14"
Expand All @@ -34,6 +33,7 @@ url = { version = "2.5.0", features = ["serde"] }
http = "1.1.0"
gitlab = "0.1706.0"
flake-schemas = "0.3.0"
walkdir = "2.5.0"

[profile.release]
strip = true # Automatically strip symbols from the binary.
Expand Down
74 changes: 42 additions & 32 deletions src/flake_info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
io::Write,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};

use color_eyre::eyre::{eyre, Result, WrapErr};
use flake_schemas::{InspectOptions, InspectOutput};
Expand Down Expand Up @@ -204,39 +201,52 @@ impl FlakeMetadata {
};
tracing::debug!("lastModified = {}", last_modified);

let mut tarball_builder = tar::Builder::new(vec![]);
let output = flate2::write::GzEncoder::new(vec![], flate2::Compression::default());
let output = std::io::BufWriter::new(output);
let mut tarball_builder = tar::Builder::new(output);
tarball_builder.follow_symlinks(false);
tarball_builder.force_mtime(last_modified);

tracing::trace!("Creating tarball");
// `tar` works according to the current directory (yay)
// So we change dir and restory it after
// TODO: Fix this
let source = &self.source_dir; // refactor to be known when we create struct with from_dir
let current_dir = std::env::current_dir().wrap_err("Could not get current directory")?;
std::env::set_current_dir(
source
.parent()
.ok_or_else(|| eyre!("Getting parent directory"))?,
)?;
let dirname = self
.source_dir
.file_name()
.ok_or_else(|| eyre!("No file name of directory"))?;
tarball_builder
.append_dir_all(dirname, dirname)
.wrap_err_with(|| eyre!("Adding `{}` to tarball", self.source_dir.display()))?;
std::env::set_current_dir(current_dir).wrap_err("Could not set current directory")?;
let parent = source
.parent()
.ok_or_else(|| eyre!("Source dir had no parent, cannot continue"))?;

tracing::trace!("Creating compressed tarball");
for entry in walkdir::WalkDir::new(source).sort_by_file_name() {
let entry = entry?;
let path = entry.path();
let subpath = path.strip_prefix(parent)?;

let metadata = path.symlink_metadata()?;

let mut header = tar::Header::new_gnu();
header.set_metadata_in_mode(&metadata, tar::HeaderMode::Deterministic);
header.set_mtime(last_modified);
header.set_uid(0);
header.set_gid(0);
Comment on lines +223 to +226
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot May 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🌐 Web query:

tar crate 0.4.46 HeaderMode Deterministic Complete set_metadata_in_mode preserves mode bits 0o755 0o644

💡 Result:

In the tar crate (version 0.4.46), the HeaderMode::Deterministic variant is designed to produce reproducible archive headers by excluding metadata that changes between builds, such as specific ownership (UID/GID) and timestamps [1][2]. When set_metadata_in_mode is called with HeaderMode::Deterministic, the crate handles file mode bits explicitly to maintain a standard, deterministic state [2]: 1. Ownership: UID and GID are set to 0 [2]. 2. Timestamp: The mtime is set to a constant DETERMINISTIC_TIMESTAMP to avoid issues with zero-value timestamps in some tools [3][2]. 3. Mode Bits: The crate does not simply copy the raw mode bits from the source filesystem [2]. Instead, it applies logic to normalize them [2]: - It checks if the file is a directory or if the user-execute bit (0o100) is set on the original file [2]. - If either condition is true, it sets the mode to 0o755 [2]. - Otherwise, it sets the mode to 0o644 [2]. This mechanism ensures that the resulting tar headers have predictable, consistent permissions (0o755 or 0o644) regardless of the specific umask or file permissions present on the system where the archive is being created [2]. In contrast, HeaderMode::Complete preserves the original metadata, including full ownership and mode bits, from the filesystem [2].

Citations:


Preserve original permission bits in tar headers

tar::HeaderMode::Deterministic normalizes archived mode bits (directories/executables -> 0o755, others -> 0o644) instead of preserving the filesystem/store modes, which can widen permissions versus what you started with. Since Lines 224-226 already overwrite mtime/uid/gid, switch to HeaderMode::Complete to preserve the metadata-provided mode bits while keeping those fields controlled.

Suggested change
-            header.set_metadata_in_mode(&metadata, tar::HeaderMode::Deterministic);
+            header.set_metadata_in_mode(&metadata, tar::HeaderMode::Complete);
             header.set_mtime(last_modified);
             header.set_uid(0);
             header.set_gid(0);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
header.set_metadata_in_mode(&metadata, tar::HeaderMode::Deterministic);
header.set_mtime(last_modified);
header.set_uid(0);
header.set_gid(0);
header.set_metadata_in_mode(&metadata, tar::HeaderMode::Complete);
header.set_mtime(last_modified);
header.set_uid(0);
header.set_gid(0);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/flake_info.rs` around lines 223 - 226, The tar header currently uses
header.set_metadata_in_mode(&metadata, tar::HeaderMode::Deterministic) which
normalizes mode bits; change it to use tar::HeaderMode::Complete so the mode
from metadata is preserved while still explicitly setting mtime/uid/gid via
header.set_mtime(last_modified), header.set_uid(0), and header.set_gid(0);
update the call to set_metadata_in_mode accordingly to retain original
permission bits from metadata.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah. Honestly, I'd rather it do the equivalent of chmod ugo-w on everything but Nix already does that for us when it extracts the flake to the store so...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cole-h, understood — since Nix enforces read-only store permissions at extraction time regardless of what's in the tarball, the wider 0o755/0o644 modes from HeaderMode::Deterministic are harmless in practice. Makes sense to leave it as-is.

(ᵔᴥᵔ)


✏️ Learnings added
Learnt from: cole-h
Repo: DeterminateSystems/flakehub-push PR: 317
File: src/flake_info.rs:223-226
Timestamp: 2026-05-26T20:56:40.994Z
Learning: In `src/flake_info.rs` (Rust), `FlakeMetadata::flake_tarball` intentionally uses `tar::HeaderMode::Deterministic` when building the flake tarball. This normalizes mode bits to `0o755` for directories/executables and `0o644` for other files, which is "wider" than Nix store permissions. This is acceptable because Nix enforces the correct read-only permissions (dr-xr-xr-x / -r--r--r--) when it extracts the tarball into the Nix store, so the tarball's wider permissions are harmless. Do not suggest switching to `HeaderMode::Complete` to preserve original mode bits.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


if metadata.is_dir() {
tarball_builder.append_data(&mut header, subpath, std::io::Cursor::new([]))?;
} else if metadata.is_file() {
let src = std::fs::File::open(path).map(std::io::BufReader::new)?;
tarball_builder.append_data(&mut header, subpath, src)?;
} else if metadata.is_symlink() {
let target = path.read_link()?;
tarball_builder.append_link(&mut header, subpath, target)?;
} else {
tracing::warn!(?path, "Ignoring unexpected special file");
continue;
}
}

let tarball = tarball_builder.into_inner().wrap_err("Creating tarball")?;
tracing::trace!("Created tarball, compressing...");
let mut gzip_encoder =
flate2::write::GzEncoder::new(vec![], flate2::Compression::default());
gzip_encoder
.write_all(&tarball[..])
.wrap_err("Adding tarball to gzip")?;
let compressed_tarball = gzip_encoder.finish().wrap_err("Creating gzip")?;
tracing::trace!("Compressed tarball");
tracing::trace!("Created tarball, finishing compression...");
let compressed_tarball = tarball
.into_inner()
.wrap_err("Creating gzip")?
.finish()
.wrap_err("Finalizing compression")?;
tracing::trace!("Finished tarball");

let flake_tarball_hash = {
let mut context = ring::digest::Context::new(&ring::digest::SHA256);
Expand Down
Loading