Skip to content

Commit 0e1f8ff

Browse files
committed
Support $HOME/.config/build-wrap/allow.txt
1 parent 0d57e46 commit 0e1f8ff

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ once_cell = "1.20"
1515
regex = "1.11"
1616
tempfile = "3.14"
1717
toml = "0.8"
18+
xdg = "2.5"
1819

1920
[dev-dependencies]
2021
assert_cmd = "2.0"

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Note that the below environment variables are read **when a build script is link
4242
BUILD_WRAP_ALLOW=1 cargo build -vv
4343
```
4444

45+
If a package must always be built with this strategy, put the package's name in [`$HOME/.config/build-wrap/allow.txt`] (see below).
46+
4547
- `BUILD_WRAP_CMD`: Command used to execute a build script. Linux default:
4648

4749
- With comments:
@@ -90,6 +92,17 @@ Note that the below environment variables are read **when a build script is link
9092
(deny network*) ;; Deny network access
9193
```
9294
95+
## `$HOME/.config/build-wrap/allow.txt`
96+
97+
If a file at `$HOME/.config/build-wrap/allow.txt` exists, `build-wrap` treats each line as the name of a package. Such packages are built as though `BUILD_WRAP_ALLOW` were set to `1`.
98+
99+
For example, [`svm-rs-builds`] downloads information about Solc releases when it is built. So if you build [`svm-rs`] frequently, you might do the following:
100+
101+
```sh
102+
mkdir -p "$HOME/.config/build-wrap"
103+
echo 'svm-rs-builds' > "$HOME/.config/build-wrap/allow.txt"
104+
```
105+
93106
## Environment variables that `build-wrap` treats as set
94107

95108
Note that we say "treats as set" because these are considered only when [`BUILD_WRAP_CMD` is expanded].
@@ -134,9 +147,12 @@ The "wrapped" version of the build script does the following when invoked:
134147
[How `build-wrap` works]: #how-build-wrap-works
135148
[Ubuntu Community Wiki]: https://help.ubuntu.com/community/AppArmor
136149
[Ubuntu Server]: https://documentation.ubuntu.com/server/how-to/security/apparmor/
150+
[`$HOME/.config/build-wrap/allow.txt`]: #homeconfigbuild-wrapallowtxt
137151
[`BUILD_WRAP_CMD` is expanded]: #how-build_wrap_cmd-is-expanded
138152
[`cc-rs`]: https://github.com/rust-lang/cc-rs
139153
[`sandbox-exec`]: https://keith.github.io/xcode-man-pages/sandbox-exec.1.html
154+
[`svm-rs-builds`]: https://github.com/alloy-rs/svm-rs/tree/master/crates/svm-builds
155+
[`svm-rs`]: https://github.com/alloy-rs/svm-rs
140156
[affect Bubblewrap]: https://github.com/containers/bubblewrap/issues/505#issuecomment-2093203129
141157
[as it would `BUILD_WRAP_CMD`]: #how-build_wrap_cmd-is-expanded
142158
[changed with version 24.04]: https://ubuntu.com/blog/ubuntu-23-10-restricted-unprivileged-user-namespaces

src/util/common.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::{anyhow, bail, ensure, Context, Result};
44
use once_cell::sync::Lazy;
55
use std::{
66
env,
7-
fs::canonicalize,
7+
fs::{canonicalize, read_to_string},
88
io::Write,
99
os::unix::ffi::OsStrExt,
1010
path::Path,
@@ -84,7 +84,7 @@ fn exec_sibling(sibling_path_as_str: &str) -> Result<()> {
8484
// They will cause the wrapped build script to be rerun, however.
8585
let expanded_args = split_and_expand(sibling_path)?;
8686

87-
let allow_enabled = enabled("BUILD_WRAP_ALLOW");
87+
let allow_enabled = enabled("BUILD_WRAP_ALLOW") || package_name_allowed();
8888

8989
let mut command = Command::new(&expanded_args[0]);
9090
command.args(&expanded_args[1..]);
@@ -275,6 +275,22 @@ fn enabled(name: &str) -> bool {
275275
env::var(name).is_ok_and(|value| value != "0")
276276
}
277277

278+
static ALLOWED_PACKAGE_NAMES: Lazy<Vec<String>> = Lazy::new(|| {
279+
let base_directories = xdg::BaseDirectories::new().unwrap();
280+
let Some(allowed) = base_directories.find_config_file("build-wrap/allow.txt") else {
281+
return Vec::new();
282+
};
283+
let contents = read_to_string(allowed).unwrap();
284+
contents.lines().map(ToOwned::to_owned).collect()
285+
});
286+
287+
fn package_name_allowed() -> bool {
288+
let Ok(package_name) = env::var("CARGO_PKG_NAME") else {
289+
return false;
290+
};
291+
ALLOWED_PACKAGE_NAMES.contains(&package_name)
292+
}
293+
278294
#[cfg(test)]
279295
pub use test::assert_readme_contains_code_block;
280296

src/wrapper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ publish = false
4444
anyhow = "1.0"
4545
once_cell = "1.19"
4646
tempfile = "3.10"
47+
xdg = "2.5"
4748
"#;
4849

4950
/// A wrapper build script's src/main.rs consists of the following:

0 commit comments

Comments
 (0)