-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f019b5
commit 441e41a
Showing
12 changed files
with
696 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# | ||
# Helper Makefile to build base image | ||
# | ||
# - local build / debug: | ||
# make base-image | ||
# | ||
# - release packaging: | ||
# make base-image package=yes compress=yes build_profile=release | ||
# | ||
# - cross build: | ||
# make base-image build_arch={aarch64 | x86_64} | ||
# | ||
# When building without packaging, dive can then be run with: | ||
# dive -i nix/ [container name] | ||
# | ||
|
||
native_arch = $(shell uname -m) | ||
|
||
# aarch64 | x86_64 | ||
build_arch ?= $(native_arch) | ||
|
||
# debug | release | ||
build_profile ?= debug | ||
|
||
# yes | no | ||
package ?= no | ||
|
||
# yes | no | ||
compress ?= no | ||
|
||
cargo_target ?= $(build_arch)-unknown-linux-musl | ||
|
||
ifeq ($(build_profile),debug) | ||
cargo_profile = dev | ||
else ifeq ($(build_profile),release) | ||
cargo_profile = release | ||
else | ||
$(error unknown cargo build profile "$(build_profile)") | ||
endif | ||
|
||
pkg_bin = target/$(cargo_target)/$(build_profile)/pkg | ||
|
||
build_img = cargo run --bin build-img -- | ||
build_img_args = -a $(build_arch) -b $(pkg_bin) | ||
|
||
ifeq ($(package),no) | ||
build_img_args += -p nix --unpackaged | ||
else ifeq ($(compress),no) | ||
build_img_args += --uncompressed | ||
endif | ||
|
||
cargo_build = cargo build --profile $(cargo_profile) --target $(cargo_target) | ||
|
||
.PHONY: clean dist-clean base-image pkg-bin $(pkg_bin) | ||
|
||
base_files = base.sha256 base.tar base.tar.xz | ||
|
||
clean: | ||
@echo "Removing base files" | ||
@rm -f $(base_files) | ||
@echo "Removing nix directory" | ||
@chmod -R +w nix/* 2>/dev/null ; rm -rf ./nix | ||
|
||
dist-clean: clean | ||
@echo "Removing rust builds" | ||
@rm -rf target | ||
|
||
base-image: pkg-bin | ||
@echo "Building base image" | ||
$(build_img) $(build_img_args) | ||
|
||
pkg-bin: $(pkg_bin) | ||
|
||
$(pkg_bin): | ||
@echo "Building pkg tool" | ||
$(cargo_build) --bin pkg | ||
@echo "Stripping debug info" | ||
strip $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::process; | ||
|
||
use anstream::{eprintln, println}; | ||
use anyhow::{Context, Result}; | ||
use clap::{Parser, Subcommand}; | ||
use owo_colors::OwoColorize; | ||
|
||
#[derive(Parser)] | ||
#[command(version, about, long_about = None)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
enum Commands { | ||
/// List installed packages | ||
List, | ||
/// Search packages | ||
Search { name: String }, | ||
/// Install a package | ||
Install { name: String }, | ||
/// Remove a package | ||
Remove { name: String }, | ||
} | ||
|
||
fn init_logging() { | ||
env_logger::Builder::new() | ||
.filter_level(log::LevelFilter::Info) | ||
.parse_env("LOGLEVEL") | ||
.format_timestamp(None) | ||
.format_target(false) | ||
.init(); | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let cli = Cli::parse(); | ||
init_logging(); | ||
|
||
match &cli.command { | ||
Commands::List => { | ||
for pkg in dive::nixpkgs::all_packages_sorted()? { | ||
println!("{} ({})", pkg.name.bold(), pkg.version.dimmed()); | ||
} | ||
} | ||
Commands::Search { name } => { | ||
let matches = dive::nixpkgs::query([name])?; | ||
for pkg in matches { | ||
println!("* {} ({})", pkg.name.bold(), pkg.version.dimmed()); | ||
if let Some(description) = pkg.description { | ||
println!(" {}", description); | ||
} | ||
println!(); | ||
} | ||
} | ||
Commands::Install { name } => { | ||
let pkgs = dive::nixpkgs::all_packages_sorted()?; | ||
if pkgs.iter().any(|p| &p.name == name) { | ||
eprintln!("error: '{}' is already installed", name); | ||
process::exit(1); | ||
} | ||
match dive::nixpkgs::find_package(name)? { | ||
None => { | ||
eprintln!("error: '{}' does not exist", name); | ||
process::exit(1); | ||
} | ||
Some(pkg) => { | ||
if let Err(err) = dive::nixpkgs::install_package(pkg) | ||
.context("failed to install package") | ||
{ | ||
eprintln!("error: {err}"); | ||
process::exit(1); | ||
} | ||
} | ||
} | ||
} | ||
Commands::Remove { name } => { | ||
let pkgs = dive::nixpkgs::all_packages_sorted()?; | ||
let maybe_pkg = pkgs.iter().find(|p| &p.name == name); | ||
if maybe_pkg.is_none() { | ||
eprintln!("error: '{}' is not installed", name); | ||
process::exit(1); | ||
} | ||
let pkg = maybe_pkg.unwrap(); | ||
if pkg.is_builtin() { | ||
eprintln!( | ||
"Error: '{}' is a built-in package and cannot be removed", | ||
name | ||
); | ||
process::exit(1); | ||
} | ||
return dive::nixpkgs::remove_package(name) | ||
.context("failed to remove package"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.