-
Notifications
You must be signed in to change notification settings - Fork 57
feat: add init and upload in kernel-builder cli #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drbh
wants to merge
8
commits into
main
Choose a base branch
from
kernel-builder-init-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fb0a3b5
feat: support init in builder cli
drbh 368adad
feat: add upload support
drbh c35424a
fix: use cwd when init called within a dir
drbh a314749
feat: add build that wraps nix commands
drbh 47a3b36
fix: clippy clean up
drbh ab13016
fix: update minijinja apis
drbh bf97126
fix: lock rust dep hashes
drbh 2f14b74
fix: improve getting started
drbh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,89 @@ | ||
| use std::{fmt::Display, path::PathBuf, str::FromStr}; | ||
|
|
||
| use clap::Args; | ||
| use eyre::{bail, Context, Result}; | ||
|
|
||
| #[derive(Clone, Debug, Default)] | ||
| pub enum BuildTarget { | ||
| #[default] | ||
| BuildAndCopy, | ||
| BuildAndUpload, | ||
| Ci, | ||
| Default, | ||
| } | ||
|
|
||
| impl Display for BuildTarget { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match self { | ||
| BuildTarget::BuildAndCopy => write!(f, "build-and-copy"), | ||
| BuildTarget::BuildAndUpload => write!(f, "build-and-upload"), | ||
| BuildTarget::Ci => write!(f, "ci"), | ||
| BuildTarget::Default => write!(f, "default"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for BuildTarget { | ||
| type Err = String; | ||
|
|
||
| fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { | ||
| match s { | ||
| "build-and-copy" => Ok(BuildTarget::BuildAndCopy), | ||
| "build-and-upload" => Ok(BuildTarget::BuildAndUpload), | ||
| "ci" => Ok(BuildTarget::Ci), | ||
| "default" => Ok(BuildTarget::Default), | ||
| _ => Err(format!( | ||
| "unknown target `{s}`, expected one of: build-and-copy, build-and-upload, ci, default" | ||
| )), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Args)] | ||
| pub struct BuildArgs { | ||
| /// Directory of the kernel project (defaults to current directory). | ||
| #[arg(value_name = "KERNEL_DIR", default_value = ".")] | ||
| pub kernel_dir: PathBuf, | ||
|
|
||
| /// Nix flake target to run. | ||
| #[arg(long, default_value = "build-and-copy")] | ||
| pub target: BuildTarget, | ||
|
|
||
| /// Maximum number of Nix build jobs. | ||
| #[arg(long, default_value = "4")] | ||
| pub max_jobs: u32, | ||
|
|
||
| /// Number of CPU cores per build job. | ||
| #[arg(long, default_value = "4")] | ||
| pub cores: u32, | ||
|
|
||
| /// Additional arguments passed through to `nix run`. | ||
| #[arg(last = true)] | ||
| pub nix_args: Vec<String>, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we want this? |
||
| } | ||
|
|
||
| pub fn run_build(args: BuildArgs) -> Result<()> { | ||
| let flake_ref = format!(".#{}", args.target); | ||
|
|
||
| let mut cmd = std::process::Command::new("nix"); | ||
| cmd.args([ | ||
| "run", | ||
| "-L", | ||
| "--max-jobs", | ||
| &args.max_jobs.to_string(), | ||
| "--cores", | ||
| &args.cores.to_string(), | ||
| ]); | ||
| cmd.args(&args.nix_args); | ||
| cmd.arg(&flake_ref); | ||
| cmd.current_dir(&args.kernel_dir); | ||
|
|
||
| let status = cmd | ||
| .status() | ||
| .wrap_err("Cannot run `nix`. Is Nix installed?")?; | ||
|
|
||
| if !status.success() { | ||
| bail!("Build failed with exit code {}", status.code().unwrap_or(1)); | ||
| } | ||
| Ok(()) | ||
| } | ||
This file contains hidden or 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 hidden or 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,58 @@ | ||
| use std::{fs, path::PathBuf}; | ||
|
|
||
| use eyre::{Context, Result}; | ||
|
|
||
| /// Build a tokio runtime for one-shot async calls. | ||
| pub fn runtime() -> Result<tokio::runtime::Runtime> { | ||
| tokio::runtime::Builder::new_current_thread() | ||
| .enable_all() | ||
| .build() | ||
| .context("Cannot create async runtime") | ||
| } | ||
|
|
||
| /// Build an HF API client. | ||
| pub fn api() -> Result<huggingface_hub::HfApi> { | ||
| huggingface_hub::HfApi::new().context("Cannot create Hugging Face API client") | ||
| } | ||
|
|
||
| /// Resolve the HF username of the currently logged-in user via `whoami`. | ||
| pub fn whoami_username() -> Result<String> { | ||
| let rt = runtime()?; | ||
| let api = api()?; | ||
|
|
||
| rt.block_on(async { api.whoami().await.map(|user| user.username) }) | ||
| .map_err(|_| { | ||
| eyre::eyre!( | ||
| "Not logged in to Hugging Face. Run `hf auth login` first, \ | ||
| or use --name <owner/repo> to skip auto-detection." | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| /// Resolve the HF access token using the standard resolution order: | ||
| /// `HF_TOKEN` env → `HF_TOKEN_PATH` file → `$HF_HOME/token` → `~/.cache/huggingface/token`. | ||
| pub fn token() -> Option<String> { | ||
| if let Ok(token) = std::env::var("HF_TOKEN") { | ||
| if !token.is_empty() { | ||
| return Some(token); | ||
| } | ||
| } | ||
|
|
||
| let token_path = std::env::var("HF_TOKEN_PATH") | ||
| .map(PathBuf::from) | ||
| .unwrap_or_else(|_| { | ||
| let hf_home = std::env::var("HF_HOME") | ||
| .map(PathBuf::from) | ||
| .unwrap_or_else(|_| { | ||
| let home = std::env::var("HOME") | ||
| .or_else(|_| std::env::var("USERPROFILE")) | ||
| .unwrap_or_default(); | ||
| PathBuf::from(home).join(".cache/huggingface") | ||
| }); | ||
| hf_home.join("token") | ||
| }); | ||
|
|
||
| fs::read_to_string(token_path) | ||
| .ok() | ||
| .map(|t| t.trim().to_owned()) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think top-level
are nicer, easier to discover. I think we can use
#[command(flatten)]to share arguments between different build commands, something like: