Print nixos rules on exit

Signed-off-by: Hadi <hadi@example.com>
This commit is contained in:
Hadi
2026-05-06 14:41:50 +02:00
parent ecd12f18e0
commit 1ac92a5ace
4 changed files with 65 additions and 19 deletions
+4
View File
@@ -74,6 +74,10 @@ func extractField(rule, field string) string {
return rest[:end] return rest[:end]
} }
func NixOSRule(dev Device, status Status) string {
return fmt.Sprintf("%s id %s name \"%s\"", status, dev.VidPid, dev.Name)
}
func extractUnquoted(rule, field string) string { func extractUnquoted(rule, field string) string {
prefix := field + " " prefix := field + " "
idx := strings.Index(rule, prefix) idx := strings.Index(rule, prefix)
+1
View File
@@ -66,6 +66,7 @@ type actionItem struct {
fn func(int, bool) error fn func(int, bool) error
permanent bool permanent bool
status guard.Status status guard.Status
nixos bool
} }
func (a actionItem) Title() string { return a.label } func (a actionItem) Title() string { return a.label }
+48 -18
View File
@@ -1,6 +1,7 @@
package ui package ui
import ( import (
"fmt"
"strings" "strings"
"time" "time"
@@ -25,6 +26,7 @@ type (
devicesMsg []guard.Device devicesMsg []guard.Device
daemonStatusMsg string daemonStatusMsg string
actionMsg struct{ err error } actionMsg struct{ err error }
nixRuleMsg struct{ rule string }
) )
type Model struct { type Model struct {
@@ -38,8 +40,11 @@ type Model struct {
notice string notice string
selectedDev *guard.Device selectedDev *guard.Device
rulesManaged bool rulesManaged bool
pendingRules []string
} }
func (m Model) PendingRules() []string { return m.pendingRules }
func New() Model { func New() Model {
l := list.New(nil, deviceDelegate{}, 0, 0) l := list.New(nil, deviceDelegate{}, 0, 0)
l.SetShowHelp(false) l.SetShowHelp(false)
@@ -62,7 +67,7 @@ func New() Model {
rulesManaged := guard.IsRulesManaged() rulesManaged := guard.IsRulesManaged()
notice := "" notice := ""
if rulesManaged { if rulesManaged {
notice = "Rules managed by NixOS config: permanent actions not available." notice = "Rules managed by NixOS config: permanent actions will print NixOS rules on exit."
listKeys.AllowPerm.SetEnabled(false) listKeys.AllowPerm.SetEnabled(false)
listKeys.BlockPerm.SetEnabled(false) listKeys.BlockPerm.SetEnabled(false)
listKeys.RejectPerm.SetEnabled(false) listKeys.RejectPerm.SetEnabled(false)
@@ -82,18 +87,21 @@ func makeActionList(rulesManaged bool) list.Model {
var items []list.Item var items []list.Item
if rulesManaged { if rulesManaged {
items = []list.Item{ items = []list.Item{
actionItem{"allow", guard.AllowDevice, false, guard.Allowed}, actionItem{"allow", guard.AllowDevice, false, guard.Allowed, false},
actionItem{"block", guard.BlockDevice, false, guard.Blocked}, actionItem{"allow (perm)", nil, true, guard.Allowed, true},
actionItem{"reject", guard.RejectDevice, false, guard.Rejected}, actionItem{"block", guard.BlockDevice, false, guard.Blocked, false},
actionItem{"block (perm)", nil, true, guard.Blocked, true},
actionItem{"reject", guard.RejectDevice, false, guard.Rejected, false},
actionItem{"reject (perm)", nil, true, guard.Rejected, true},
} }
} else { } else {
items = []list.Item{ items = []list.Item{
actionItem{"allow", guard.AllowDevice, false, guard.Allowed}, actionItem{"allow", guard.AllowDevice, false, guard.Allowed, false},
actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed}, actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed, false},
actionItem{"block", guard.BlockDevice, false, guard.Blocked}, actionItem{"block", guard.BlockDevice, false, guard.Blocked, false},
actionItem{"block (permanent)", guard.BlockDevice, true, guard.Blocked}, actionItem{"block (permanent)", guard.BlockDevice, true, guard.Blocked, false},
actionItem{"reject", guard.RejectDevice, false, guard.Rejected}, actionItem{"reject", guard.RejectDevice, false, guard.Rejected, false},
actionItem{"reject (permanent)", guard.RejectDevice, true, guard.Rejected}, actionItem{"reject (permanent)", guard.RejectDevice, true, guard.Rejected, false},
} }
} }
l := list.New(items, actionDelegate{}, 24, len(items)) l := list.New(items, actionDelegate{}, 24, len(items))
@@ -135,6 +143,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.daemonStatus = string(msg) m.daemonStatus = string(msg)
return m, nil return m, nil
case nixRuleMsg:
m.state = stateList
m.selectedDev = nil
m.pendingRules = append(m.pendingRules, msg.rule)
count := len(m.pendingRules)
if count == 1 {
m.notice = "1 NixOS rule queued (printed on exit)"
} else {
m.notice = fmt.Sprintf("%d NixOS rules queued (printed on exit)", count)
}
return m, nil
case actionMsg: case actionMsg:
m.state = stateList m.state = stateList
m.selectedDev = nil m.selectedDev = nil
@@ -224,6 +244,10 @@ func (m Model) updatePopup(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
case key.Matches(msg, listKeys.Open): case key.Matches(msg, listKeys.Open):
if item := m.actionList.SelectedItem(); item != nil { if item := m.actionList.SelectedItem(); item != nil {
a := item.(actionItem) a := item.(actionItem)
if a.nixos && m.selectedDev != nil {
rule := guard.NixOSRule(*m.selectedDev, a.status)
return m, func() tea.Msg { return nixRuleMsg{rule: rule} }
}
return m, doAction(m.selectedDev.ID, a.fn, a.permanent) return m, doAction(m.selectedDev.ID, a.fn, a.permanent)
} }
} }
@@ -281,8 +305,13 @@ func (m Model) renderActionSelect() string {
title := popupTitleStyle.Foreground(color).Width(innerW).Render(dev.Name) title := popupTitleStyle.Foreground(color).Width(innerW).Render(dev.Name)
hint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("↑↓ navigate enter confirm esc cancel") hint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("↑↓ navigate enter confirm esc cancel")
content := strings.Join([]string{title, m.actionList.View(), "", hint}, "\n") parts := []string{title, m.actionList.View(), ""}
return popupStyle.Width(innerW).Render(content) if m.rulesManaged {
nixosHint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("[NixOS: perm rules printed on exit]")
parts = append(parts, nixosHint)
}
parts = append(parts, hint)
return popupStyle.Width(innerW).Render(strings.Join(parts, "\n"))
} }
func (m Model) popupOuterWidth() int { func (m Model) popupOuterWidth() int {
@@ -302,15 +331,12 @@ func (m Model) actionListInnerWidth() int {
func (m Model) defaultNotice() string { func (m Model) defaultNotice() string {
if m.rulesManaged { if m.rulesManaged {
return "Rules managed by NixOS config: permanent actions not available." return "Rules managed by NixOS config: permanent actions will print NixOS rules on exit."
} }
return "" return ""
} }
func (m Model) actionItemCount() int { func (m Model) actionItemCount() int {
if m.rulesManaged {
return 3
}
return 6 return 6
} }
@@ -321,8 +347,12 @@ func (m Model) actionItemCount() int {
func (m *Model) updateActionListSize() { func (m *Model) updateActionListSize() {
items := m.actionItemCount() items := m.actionItemCount()
innerW := m.actionListInnerWidth() innerW := m.actionListInnerWidth()
// popup overhead: border(2) + padding_v(2) + title(1) + blank(1) + hint(1) = 7 // popup overhead: border(2) + padding_v(2) + title(1) + blank(1) + hint(1) = 7; +1 for NixOS footer
available := m.height - 7 - 2 // 2 lines margin overhead := 7
if m.rulesManaged {
overhead = 8
}
available := m.height - overhead - 2 // 2 lines margin
if available >= items { if available >= items {
m.actionList.SetShowPagination(false) m.actionList.SetShowPagination(false)
m.actionList.SetSize(innerW, items) m.actionList.SetSize(innerW, items)
+12 -1
View File
@@ -23,8 +23,19 @@ func main() {
} }
p := tea.NewProgram(ui.New()) p := tea.NewProgram(ui.New())
if _, err := p.Run(); err != nil { m, err := p.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
if fm, ok := m.(ui.Model); ok {
if rules := fm.PendingRules(); len(rules) > 0 {
fmt.Println("# Add to your NixOS configuration:")
fmt.Println("services.usbguard.rules = lib.mkAfter ''")
for _, rule := range rules {
fmt.Println(" ", rule)
}
fmt.Println("'';")
}
}
} }