Skip to content

Commit

Permalink
Merge pull request #8 from snowfallorg/feat/flake-command-compat
Browse files Browse the repository at this point in the history
feat: flake command compat
  • Loading branch information
jakehamilton authored Feb 14, 2024
2 parents 2cf0254 + 2f234cb commit ac0d68c
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 1 deletion.
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,114 @@ flake switch github:jakehamilton/config#bismuth
flake switch github:jakehamilton/config --pick
```

### `flake test`

Test a system configuration

```bash
flake test

DESCRIPTION

Test a system configuration with support for both NixOS and nix-darwin.

USAGE

$ flake test <name> [options]

OPTIONS

--help, -h Show this help message
--debug Show debug messages
--show-trace Show a trace when a Nix command fails

EXAMPLES

# Test a configuration with the same hostname from the flake in the current directory.
$ flake test

# Test a specific configuration from the flake in the current directory.
$ flake test my-system

# Pick a configuration to test from the flake in the current directory.
$ flake test --pick

# Test a configuration from a specific flake.
$ flake test github:jakehamilton/config#bismuth

# Pick configuration from a specific flake.
$ flake test github:jakehamilton/config --pick
```

### `flake boot`

Update the system's bootable generations.

```bash
flake boot

DESCRIPTION

Set a NixOS configuration as the bootable system.

USAGE

$ flake boot <name> [options]

OPTIONS

--help, -h Show this help message
--debug Show debug messages
--show-trace Show a trace when a Nix command fails

EXAMPLES

# Boot a configuration with the same hostname from the flake in the current directory.
$ flake boot

# Boot a specific configuration from the flake in the current directory.
$ flake boot my-system

# Pick a configuration to boot from the flake in the current directory.
$ flake boot --pick

# Boot a configuration from a specific flake.
$ flake boot github:jakehamilton/config#bismuth

# Pick configuration from a specific flake.
$ flake boot github:jakehamilton/config --pick
```

### `flake show`

Show flake outputs.

```bash
flake show

DESCRIPTION

Show the outputs of a Nix Flake.

USAGE

$ flake show <name> [options]

OPTIONS

--help, -h Show this help message
--debug Show debug messages
--show-trace Show a trace when a Nix command fails

EXAMPLES

# Show outputs from the flake in the current directory.
$ flake show

# Show outputs from a specific flake.
$ flake show github:jakehamilton/config
```

### `flake update`

Update a Nix Flake's inputs.
Expand Down
182 changes: 181 additions & 1 deletion packages/flake/flake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ flake_switch() {
fi

if [[ "$flake_uri" != *:* ]] || [[ "$flake_uri" == *#* ]]; then
log_fatal "${text_bold}flake build --pick${text_reset} called with invalid flake uri: $flake_uri"
log_fatal "${text_bold}flake switch --pick${text_reset} called with invalid flake uri: $flake_uri"
fi

local target="nixos"
Expand Down Expand Up @@ -506,6 +506,174 @@ flake_switch() {
fi
}

flake_test() {
if [[ $opt_help == true ]]; then
show_help test
exit 0
fi

if [[ ${#positional_args[@]} > 2 ]]; then
log_fatal "${text_bold}flake test${text_reset} received too many positional arguments."
fi

if [[ $opt_pick == true ]]; then
local flake_uri=${positional_args[1]:-}

if [[ -z "${flake_uri}" ]]; then
require_flake_nix
flake_uri="path:$(pwd)"
fi

if [[ "$flake_uri" != *:* ]] || [[ "$flake_uri" == *#* ]]; then
log_fatal "${text_bold}flake test --pick${text_reset} called with invalid flake uri: $flake_uri"
fi

local target="nixos"

if [[ $is_darwin == true ]]; then
target="darwin"
fi

local raw_systems_choices=($(get_flake_attributes "${target}Configurations" "$flake_uri"))
local systems_choices=($(replace_each "path:$(pwd)" "." "${raw_systems_choices[*]}"))

if [[ ${#systems_choices[@]} == 0 ]]; then
log_fatal "Could not find any ${target} systems in flake: ${flake_uri}"
fi

log_info "Select a system:"
local system=$(gum choose \
--height=15 \
--cursor.foreground="4" \
--item.foreground="7" \
--selected.foreground="4" \
${systems_choices[*]} \
)

if [[ -z ${system} ]]; then
log_fatal "No system selected"
fi

rewrite_line "$(log_info "Select a system: ${text_fg_blue}${system}${text_reset}")"

system-rebuild test --flake "${system}"
else
if [[ ${#positional_args[@]} == 1 ]]; then
require_flake_nix
log_info "Testing system configuration .#"
system-rebuild test --flake ".#"
else
local system_name=${positional_args[1]}

if [[ "$system_name" == *:* ]] || [[ "$system_name" == *#* ]]; then
log_info "Testing system configuration ${system_name}"
system-rebuild test --flake $system_name
else
require_flake_nix
log_info "Testing system configuration .#${system_name}"
system-rebuild test --flake ".#${system_name}"
fi
fi
fi
}

flake_boot() {
if [[ $is_darwin == true ]]; then
log_fatal "${text_bold}flake boot${text_reset} is only compatible with NixOS."
exit 1
fi

if [[ $opt_help == true ]]; then
show_help boot
exit 0
fi

if [[ ${#positional_args[@]} > 2 ]]; then
log_fatal "${text_bold}flake boot${text_reset} received too many positional arguments."
fi

if [[ $opt_pick == true ]]; then
local flake_uri=${positional_args[1]:-}

if [[ -z "${flake_uri}" ]]; then
require_flake_nix
flake_uri="path:$(pwd)"
fi

if [[ "$flake_uri" != *:* ]] || [[ "$flake_uri" == *#* ]]; then
log_fatal "${text_bold}flake boot --pick${text_reset} called with invalid flake uri: $flake_uri"
fi

local target="nixos"

if [[ $is_darwin == true ]]; then
target="darwin"
fi

local raw_systems_choices=($(get_flake_attributes "${target}Configurations" "$flake_uri"))
local systems_choices=($(replace_each "path:$(pwd)" "." "${raw_systems_choices[*]}"))

if [[ ${#systems_choices[@]} == 0 ]]; then
log_fatal "Could not find any ${target} systems in flake: ${flake_uri}"
fi

log_info "Select a system:"
local system=$(gum choose \
--height=15 \
--cursor.foreground="4" \
--item.foreground="7" \
--selected.foreground="4" \
${systems_choices[*]} \
)

if [[ -z ${system} ]]; then
log_fatal "No system selected"
fi

rewrite_line "$(log_info "Select a system: ${text_fg_blue}${system}${text_reset}")"

system-rebuild boot --flake "${system}"
else
if [[ ${#positional_args[@]} == 1 ]]; then
require_flake_nix
log_info "Testing system configuration .#"
system-rebuild boot --flake ".#"
else
local system_name=${positional_args[1]}

if [[ "$system_name" == *:* ]] || [[ "$system_name" == *#* ]]; then
log_info "Testing system configuration ${system_name}"
system-rebuild boot --flake $system_name
else
require_flake_nix
log_info "Testing system configuration .#${system_name}"
system-rebuild boot --flake ".#${system_name}"
fi
fi
fi
}

flake_show() {
if [[ $opt_help == true ]]; then
show_help show
exit 0
fi
echo args: ${#positional_args[@]}

if [[ ${#positional_args[@]} > 2 ]]; then
log_fatal "${text_bold}flake show${text_reset} received too many positional arguments."
fi

if [[ ${#positional_args[@]} == 1 ]]; then
require_flake_nix
log_debug "Showing flake .#"
nix flake show .#
else
log_debug "Showing flake ${positional_args[1]}"
nix flake show ${positional_args[1]}
fi
}

flake_build() {
if [[ $opt_help == true ]]; then
show_help build
Expand Down Expand Up @@ -1037,6 +1205,18 @@ case ${positional_args[0]} in
log_debug "Running subcommand: ${text_bold}flake_switch${text_reset}"
flake_switch
;;
test)
log_debug "Running subcommand: ${text_bold}flake_test${text_reset}"
flake_test
;;
boot)
log_debug "Running subcommand: ${text_bold}flake_boot${text_reset}"
flake_boot
;;
show)
log_debug "Running subcommand: ${text_bold}flake_show${text_reset}"
flake_show
;;
build)
log_debug "Running subcommand: ${text_bold}flake_build${text_reset}"
flake_build
Expand Down
34 changes: 34 additions & 0 deletions packages/flake/help/boot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
echo -e "
${text_bold}${text_fg_blue}flake${text_reset} ${text_fg_white}boot${text_reset}
${text_bold}DESCRIPTION${text_reset}
Set a NixOS configuration as the bootable system.
${text_bold}USAGE${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake boot${text_reset} <name> [options]
${text_bold}OPTIONS${text_reset}
--help, -h Show this help message
--debug Show debug messages
--show-trace Show a trace when a Nix command fails
${text_bold}EXAMPLES${text_reset}
${text_dim}# Boot a configuration with the same hostname from the flake in the current directory.${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake boot${text_reset}
${text_dim}# Boot a specific configuration from the flake in the current directory.${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake boot${text_reset} ${text_underline}my-system${text_reset}
${text_dim}# Pick a configuration to boot from the flake in the current directory.${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake boot${text_reset} --pick
${text_dim}# Boot a configuration from a specific flake.${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake boot${text_reset} ${text_underline}github:jakehamilton/config#bismuth${text_reset}
${text_dim}# Pick configuration from a specific flake.${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake boot${text_reset} ${text_underline}github:jakehamilton/config${text_reset} --pick
"
3 changes: 3 additions & 0 deletions packages/flake/help/flake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ ${text_bold}COMMANDS${text_reset}
run Run an app
build Build a package
switch Switch system configuration
test Test a system configuration
boot Update the system's bootable generations
show Show flake outputs
update Update flake inputs
build-<target> Build a target system, see ${text_bold}flake build-system --help${text_reset}
option Show NixOS options
Expand Down
25 changes: 25 additions & 0 deletions packages/flake/help/show.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
echo -e "
${text_bold}${text_fg_blue}flake${text_reset} ${text_fg_white}show${text_reset}
${text_bold}DESCRIPTION${text_reset}
Show the outputs of a Nix Flake.
${text_bold}USAGE${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake show${text_reset} <name> [options]
${text_bold}OPTIONS${text_reset}
--help, -h Show this help message
--debug Show debug messages
--show-trace Show a trace when a Nix command fails
${text_bold}EXAMPLES${text_reset}
${text_dim}# Show outputs from the flake in the current directory.${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake show${text_reset}
${text_dim}# Show outputs from a specific flake.${text_reset}
${text_dim}\$${text_reset} ${text_bold}flake show${text_reset} ${text_underline}github:jakehamilton/config${text_reset}
"
Loading

0 comments on commit ac0d68c

Please sign in to comment.