1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
mod bundle;
mod cache;
mod compress;
mod extract;
mod info;
mod init;
mod integrate;
mod list;
mod metadata;
mod pack;
mod payload;
mod recipe;
mod run;
mod verify;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
use onelf_format::WorkingDir;
const RUNTIME_BINARY_SLIM: &[u8] = include_bytes!(env!("ONELF_RT_PATH"));
const RUNTIME_BINARY_UPDATE: &[u8] = include_bytes!(env!("ONELF_RT_UPDATE_PATH"));
#[derive(Parser)]
#[command(name = "onelf", about = "Single-binary packaging tool", version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Pack a directory into a single executable
Pack {
/// Directory to pack
directory: PathBuf,
/// Output file path
#[arg(short, long)]
output: PathBuf,
/// Relative path to the command within the directory
#[arg(long)]
command: String,
/// Package name (for identification, defaults to command basename)
#[arg(long)]
name: Option<String>,
/// Additional entrypoints (name=path)
#[arg(long, value_parser = parse_entrypoint)]
entrypoint: Vec<(String, String)>,
/// Default entrypoint name
#[arg(long)]
default_entrypoint: Option<String>,
/// Library directories to add to LD_LIBRARY_PATH (repeatable).
/// Pass "auto" (default) to detect directories containing .so files.
#[arg(long, default_values_t = [String::from("auto")])]
lib_dir: Vec<String>,
/// Zstd compression level (0-22)
#[arg(long, default_value = "12")]
level: i32,
/// Build a shared zstd dictionary
#[arg(long)]
dict: bool,
/// Store the payload uncompressed (no zstd). Larger file, but
/// zero decompression at runtime. Overrides --dict.
#[arg(long)]
no_compress: bool,
/// Mark default entrypoint as memfd-eligible
#[arg(long)]
memfd: bool,
/// Force cache mode (disable memfd)
#[arg(long)]
no_memfd: bool,
/// Working directory strategy
#[arg(long, default_value = "inherit")]
working_dir: WorkingDirArg,
/// Base URL for delta updates (stored in .onelf/update-url)
#[arg(long)]
update_url: Option<String>,
/// Path to a file holding the raw 32-byte Ed25519 public key used
/// to verify signed self-updates (stored in .onelf/update-key)
#[arg(long)]
update_key: Option<PathBuf>,
/// Exclude files matching glob patterns (repeatable, e.g. "*.a", "__pycache__")
#[arg(long)]
exclude: Vec<String>,
/// Library to dlopen on every exec via the onelf-env constructor
/// (repeatable). Survives sandboxed re-exec, unlike LD_PRELOAD.
#[arg(long)]
preload: Vec<String>,
/// Pin every entry's mtime to this Unix timestamp for fully
/// reproducible output, independent of filesystem timestamps.
#[arg(long)]
mtime: Option<u64>,
},
/// Scaffold a starter onelf.toml
Init {
/// Path to write the recipe to
#[arg(short, long, default_value = "onelf.toml")]
output: PathBuf,
/// Seed the recipe from a binary: sets name/command from its basename
#[arg(long, value_name = "PATH")]
binary: Option<PathBuf>,
/// Overwrite an existing file
#[arg(long)]
force: bool,
},
/// Build from an onelf.toml recipe (runs bundle-libs + pack)
Build {
/// Path to onelf.toml, or to the directory containing it (default: .)
#[arg(default_value = ".")]
path: PathBuf,
/// Override the output path from the recipe
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Run an AppDir in place, without packing, for fast dev iteration
Run {
/// AppDir path, or a .toml recipe file (default: .)
#[arg(default_value = ".")]
path: PathBuf,
/// Path to the binary to exec, relative to the AppDir (overrides any
/// recipe-specified command). Handy when there's no onelf.toml.
#[arg(long)]
command: Option<String>,
/// Entrypoint name to run (default: the recipe's default entrypoint)
#[arg(long)]
entrypoint: Option<String>,
/// Run bundle-libs first using the recipe's [bundle] settings. Useful
/// for a one-shot dev loop; does nothing if the AppDir has no recipe.
#[arg(long)]
bundle: bool,
/// Arguments passed to the entrypoint after its own args
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec<String>,
},
/// Verify a packed binary's integrity (recompute hashes vs manifest)
Verify {
/// Path to the onelf binary
binary: PathBuf,
},
/// Show metadata about a packed binary
Info {
/// Path to the onelf binary
binary: PathBuf,
},
/// List all files in a packed binary
List {
/// Path to the onelf binary
binary: PathBuf,
},
/// Extract files from a packed binary
Extract {
/// Path to the onelf binary
binary: PathBuf,
/// Output path (directory, file, or "-" for stdout)
#[arg(short, long)]
output: Option<PathBuf>,
/// Extract only specific files by path (repeatable)
#[arg(long)]
file: Vec<String>,
/// Preserve full mode bits including setuid/setgid/sticky.
/// Unsafe for untrusted packages; off by default (masked to 0o777).
#[arg(long)]
preserve_mode: bool,
},
/// Extract icon from a packed binary
Icon {
/// Path to the onelf binary
binary: PathBuf,
/// Entrypoint name (default: default entrypoint)
#[arg(long)]
entrypoint: Option<String>,
/// Output path (default: stdout)
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Extract desktop file from a packed binary
Desktop {
/// Path to the onelf binary
binary: PathBuf,
/// Entrypoint name (default: default entrypoint)
#[arg(long)]
entrypoint: Option<String>,
/// Output path (default: stdout)
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Install desktop shortcut and icon for a packed binary (XDG integration)
Integrate {
/// Path to the onelf binary
binary: PathBuf,
/// Entrypoint name (default: default entrypoint)
#[arg(long)]
entrypoint: Option<String>,
},
/// Remove desktop shortcut and icon installed by integrate
Unintegrate {
/// Path to the onelf binary
binary: PathBuf,
/// Entrypoint name (default: default entrypoint)
#[arg(long)]
entrypoint: Option<String>,
},
/// Manage the onelf cache
Cache {
#[command(subcommand)]
action: CacheAction,
},
/// Bundle shared library dependencies into a directory
BundleLibs {
/// Directory containing the application to bundle. When used with
/// --from-binary, this is the output directory (created if needed).
directory: PathBuf,
/// Specific binary to analyze (default: scan all ELF files)
#[arg(long)]
target: Option<PathBuf>,
/// Scaffold a fresh AppDir: copy this binary into <DIRECTORY>/bin/
/// (creating the directory) before bundling its dependencies.
#[arg(long, value_name = "PATH")]
from_binary: Option<PathBuf>,
/// Where to copy libs, relative to DIRECTORY
#[arg(long, default_value = "lib")]
lib_dir: PathBuf,
/// Exclude libs matching pattern (prefix match, comma-separated or repeatable)
#[arg(long, value_delimiter = ',')]
exclude: Vec<String>,
/// Additional libraries to include by soname (e.g. dlopen'd libs, comma-separated or repeatable)
#[arg(long, value_delimiter = ',')]
include: Vec<String>,
/// Additional directories to search for libraries (repeatable)
#[arg(long)]
search_path: Vec<PathBuf>,
/// Show what would be copied without copying
#[arg(long)]
dry_run: bool,
/// Don't resolve transitive dependencies
#[arg(long)]
no_recursive: bool,
/// Bundle Mesa GL/EGL/GBM libraries
#[arg(long)]
gl: bool,
/// Bundle DRI drivers (architecture-filtered)
#[arg(long)]
dri: bool,
/// Bundle Vulkan ICD drivers (architecture-filtered)
#[arg(long)]
vulkan: bool,
/// Bundle Wayland client libraries (libwayland, libdecor, libxkbcommon)
#[arg(long)]
wayland: bool,
/// Bundle GSettings schemas for GTK apps
#[arg(long)]
gtk: bool,
/// Do not bundle Mesa GL/EGL/GBM libraries even if auto-detected
#[arg(long)]
no_gl: bool,
/// Do not bundle DRI drivers even if auto-detected
#[arg(long)]
no_dri: bool,
/// Do not bundle Vulkan ICD drivers even if auto-detected
#[arg(long)]
no_vulkan: bool,
/// Do not bundle Wayland client libraries even if auto-detected
#[arg(long)]
no_wayland: bool,
/// Do not bundle GSettings schemas even if auto-detected
#[arg(long)]
no_gtk: bool,
/// Strip debug symbols from copied libraries
#[arg(long)]
strip: bool,
/// Skip libraries whose libc family (musl vs glibc) doesn't match
/// the target binary. Without this flag, mismatched libs are copied
/// with a warning.
#[arg(long)]
strict_libc: bool,
/// Scan binary strings for soname-shaped values that match a known
/// allow-list of commonly dlopen'd libraries (GL, Wayland, Vulkan,
/// audio, DBus, etc.) and bundle the matches.
#[arg(long)]
scan_dlopen: bool,
/// Extra sonames added to the --scan-dlopen allow-list (repeatable
/// or comma-separated). Matches must still appear in the binary's
/// strings to be bundled.
#[arg(long, value_delimiter = ',')]
dlopen: Vec<String>,
},
}
#[derive(Subcommand)]
enum CacheAction {
/// List cached packages
List,
/// Remove all cached data
Clear,
/// Garbage collect old cache entries
Gc {
/// Maximum age in days
#[arg(long, default_value = "30")]
max_age: u64,
},
}
#[derive(Clone, ValueEnum)]
enum WorkingDirArg {
Inherit,
Package,
Command,
}
fn parse_entrypoint(s: &str) -> Result<(String, String), String> {
let (name, path) = s.split_once('=').ok_or("expected format: name=path")?;
Ok((name.to_string(), path.to_string()))
}
fn main() {
let cli = Cli::parse();
let result = match cli.command {
Commands::Pack {
directory,
output,
command,
name,
entrypoint,
default_entrypoint,
lib_dir,
level,
dict,
no_compress,
memfd,
no_memfd,
working_dir,
update_url,
update_key,
exclude,
preload,
mtime,
} => {
let memfd_opt = if no_memfd {
Some(false)
} else if memfd {
Some(true)
} else {
None
};
let wd = match working_dir {
WorkingDirArg::Inherit => WorkingDir::Inherit,
WorkingDirArg::Package => WorkingDir::PackageRoot,
WorkingDirArg::Command => WorkingDir::EntrypointParent,
};
let entrypoints = entrypoint
.into_iter()
.map(|(n, p)| (n, p, Vec::new()))
.collect();
let update_key = update_key.as_deref().map(std::fs::read).transpose();
match update_key {
Err(e) => Err(e),
Ok(update_key) => pack::pack(
&pack::PackOptions {
directory,
output,
command,
name,
entrypoints,
default_entrypoint,
lib_dirs: lib_dir,
level,
use_dict: dict,
no_compress,
memfd: memfd_opt,
working_dir: wd,
update_url: update_url.clone(),
update_key,
exclude,
package_info: None,
mtime,
env: Vec::new(),
preload,
},
// Pick the runtime: slim (~700KB) by default; the
// update-capable runtime (~2MB) only when the user actually
// configures self-updates.
if update_url.is_some() {
RUNTIME_BINARY_UPDATE
} else {
RUNTIME_BINARY_SLIM
},
),
}
}
Commands::Init {
output,
binary,
force,
} => init::init(&output, binary.as_deref(), force),
Commands::Build { path, output } => run_build(&path, output.as_deref()),
Commands::Run {
path,
command,
entrypoint,
bundle,
args,
} => run::run(
&path,
command.as_deref(),
entrypoint.as_deref(),
bundle,
&args,
),
Commands::Verify { binary } => verify::verify(&binary),
Commands::Info { binary } => info::info(&binary),
Commands::List { binary } => list::list(&binary),
Commands::Extract {
binary,
output,
file,
preserve_mode,
} => extract::extract(&binary, output.as_deref(), &file, preserve_mode),
Commands::Icon {
binary,
entrypoint,
output,
} => metadata::icon(&binary, entrypoint.as_deref(), output.as_deref()),
Commands::Desktop {
binary,
entrypoint,
output,
} => metadata::desktop(&binary, entrypoint.as_deref(), output.as_deref()),
Commands::Integrate { binary, entrypoint } => {
integrate::integrate(&binary, entrypoint.as_deref())
}
Commands::Unintegrate { binary, entrypoint } => {
integrate::unintegrate(&binary, entrypoint.as_deref())
}
Commands::Cache { action } => match action {
CacheAction::List => cache::cache_list(),
CacheAction::Clear => cache::cache_clear(),
CacheAction::Gc { max_age } => cache::cache_gc(max_age),
},
Commands::BundleLibs {
directory,
target,
from_binary,
lib_dir,
exclude,
include,
search_path,
dry_run,
no_recursive,
gl,
dri,
vulkan,
wayland,
gtk,
no_gl,
no_dri,
no_vulkan,
no_wayland,
no_gtk,
strip,
strict_libc,
scan_dlopen,
dlopen,
} => scaffold_from_binary(&directory, from_binary.as_deref()).and_then(|_| {
bundle::bundle_libs(&bundle::BundleOptions {
directory,
target,
lib_dir,
exclude,
include,
search_path,
dry_run,
recursive: !no_recursive,
gl,
dri,
vulkan,
wayland,
gtk,
no_gl,
no_dri,
no_vulkan,
no_wayland,
no_gtk,
strip,
strict_libc,
scan_dlopen,
dlopen_extra: dlopen,
})
}),
};
if let Err(e) = result {
eprintln!("error: {e}");
std::process::exit(1);
}
}
fn run_build(
path: &std::path::Path,
output_override: Option<&std::path::Path>,
) -> std::io::Result<()> {
let recipe_path = recipe::resolve(path)?;
let recipe = recipe::load(&recipe_path)?;
let dir = recipe_path
.parent()
.map(std::path::Path::to_path_buf)
.unwrap_or_else(|| std::path::PathBuf::from("."));
// Stage 1: bundle-libs
if !recipe.bundle.skip {
let search_path: Vec<PathBuf> = recipe
.bundle
.search_paths
.iter()
.map(|s| PathBuf::from(s.as_str()))
.collect();
bundle::bundle_libs(&bundle::BundleOptions {
directory: dir.clone(),
target: None,
lib_dir: PathBuf::from("lib"),
exclude: recipe.bundle.exclude.clone(),
include: recipe.bundle.include.clone(),
search_path,
dry_run: false,
recursive: true,
gl: recipe.bundle.gl,
dri: recipe.bundle.dri,
vulkan: recipe.bundle.vulkan,
wayland: recipe.bundle.wayland,
gtk: recipe.bundle.gtk,
no_gl: recipe.bundle.no_gl,
no_dri: recipe.bundle.no_dri,
no_vulkan: recipe.bundle.no_vulkan,
no_wayland: recipe.bundle.no_wayland,
no_gtk: recipe.bundle.no_gtk,
strip: recipe.bundle.strip,
strict_libc: recipe.bundle.strict_libc,
scan_dlopen: recipe.bundle.scan_dlopen,
dlopen_extra: recipe.bundle.dlopen.clone(),
})?;
}
// Stage 2: pack. Relative output paths in the recipe are resolved
// against the recipe's directory.
let output = match output_override {
Some(o) => o.to_path_buf(),
None => match recipe.package.output.clone() {
Some(o) if o.is_absolute() => o,
Some(o) => dir.join(o),
None => {
let name = recipe.package.name.clone().unwrap_or_else(|| {
recipe
.package
.command
.rsplit('/')
.next()
.unwrap_or("app")
.to_string()
});
dir.join(format!("{name}.onelf"))
}
},
};
let entrypoints: Vec<(String, String, Vec<String>)> = recipe
.entrypoint
.iter()
.map(|e| (e.name.clone(), e.path.clone(), e.args.clone()))
.collect();
let default_entrypoint = recipe
.entrypoint
.iter()
.find(|e| e.default)
.map(|e| e.name.clone());
let lib_dirs = recipe
.bundle
.lib_dirs
.clone()
.unwrap_or_else(|| vec!["auto".to_string()]);
let update_url = recipe.update.as_ref().map(|u| u.url.clone());
let update_key = recipe
.update
.as_ref()
.and_then(|u| u.key.clone())
.map(|k| if k.is_absolute() { k } else { dir.join(k) })
.map(std::fs::read)
.transpose()?;
let runtime: &[u8] = if update_url.is_some() {
RUNTIME_BINARY_UPDATE
} else {
RUNTIME_BINARY_SLIM
};
let package_info = build_package_info(&recipe.package);
pack::pack(
&pack::PackOptions {
directory: dir,
output,
command: recipe.package.command,
name: recipe.package.name,
entrypoints,
default_entrypoint,
lib_dirs,
level: recipe.compression.level,
use_dict: recipe.compression.dict,
no_compress: recipe.compression.store,
memfd: recipe.package.memfd,
working_dir: recipe.package.working_dir.into(),
update_url,
update_key,
exclude: recipe.package.exclude,
package_info,
mtime: recipe.package.mtime,
env: recipe.env.into_iter().collect(),
preload: recipe.preload,
},
runtime,
)
}
/// Serialize optional package metadata fields to TOML. Returns None if no
/// metadata is present.
fn build_package_info(pkg: &recipe::Package) -> Option<String> {
let mut lines = Vec::new();
if let Some(ref s) = pkg.name {
lines.push(format!("name = {}", toml_str(s)));
}
if let Some(ref s) = pkg.version {
lines.push(format!("version = {}", toml_str(s)));
}
if let Some(ref s) = pkg.description {
lines.push(format!("description = {}", toml_str(s)));
}
if let Some(ref s) = pkg.license {
lines.push(format!("license = {}", toml_str(s)));
}
if let Some(ref s) = pkg.homepage {
lines.push(format!("homepage = {}", toml_str(s)));
}
if lines.is_empty() {
None
} else {
Some(lines.join("\n") + "\n")
}
}
/// Quote `s` as a TOML basic string, escaping backslash, double quote, and
/// control characters (per the TOML spec) so a value containing a newline or
/// other control char cannot produce a malformed `.onelf/package-info.toml`.
fn toml_str(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
out.push('"');
for c in s.chars() {
match c {
'\\' => out.push_str("\\\\"),
'"' => out.push_str("\\\""),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c if (c as u32) < 0x20 || c == '\u{7f}' => {
out.push_str(&format!("\\u{:04X}", c as u32));
}
c => out.push(c),
}
}
out.push('"');
out
}
/// If `src` is given, copy it into `dir/bin/<basename>`, creating `dir/bin`
/// first. No-op when `src` is None.
fn scaffold_from_binary(
dir: &std::path::Path,
src: Option<&std::path::Path>,
) -> std::io::Result<()> {
let Some(src) = src else {
return Ok(());
};
if !src.is_file() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("--from-binary: {} is not a file", src.display()),
));
}
let bin_dir = dir.join("bin");
std::fs::create_dir_all(&bin_dir)?;
let name = src.file_name().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"--from-binary has no filename",
)
})?;
let dest = bin_dir.join(name);
std::fs::copy(src, &dest)?;
std::fs::set_permissions(&dest, std::fs::Permissions::from_mode(0o755))?;
eprintln!("Scaffolded {} -> {}", src.display(), dest.display());
Ok(())
}