❄ Personal NixOS Flake Manager
nixos home-manager go nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(rui): more home-manager and nixos-rebuild options

Fuwn 77b565b7 a7eb4975

+138 -59
+5 -4
README.md
··· 97 97 Fuwn <contact@fuwn.me> 98 98 99 99 COMMANDS: 100 - home 101 - os 102 - edit 100 + home 101 + os 102 + edit 103 103 help, h Shows a list of commands or help for one command 104 104 105 105 GLOBAL OPTIONS: 106 - --help, -h show help 106 + --allow-unfree (default: false) 107 + --help, -h show help 107 108 108 109 COPYRIGHT: 109 110 Copyright (c) 2024-2024 Fuwn
+133 -55
rui.go
··· 16 16 Flake string `json:"flake"` 17 17 } 18 18 19 + type ActionDetails struct { 20 + Name string 21 + Verb string 22 + UsableWithNH bool 23 + } 24 + 19 25 var configuration Configuration 20 26 21 27 const ( 22 - Build = iota 23 - Switch 28 + Switch = iota 29 + Boot 30 + Test 31 + Build 32 + DryActivate 33 + BuildVM 34 + Instantiate 35 + Generations 36 + Packages 24 37 ) 25 38 26 39 func init() { ··· 41 54 } 42 55 } 43 56 57 + func subcommand(action int, aliases []string, flags []cli.Flag, commandAction func(c *cli.Context, action int) error) *cli.Command { 58 + return &cli.Command{ 59 + Name: actionName(action), 60 + Aliases: aliases, 61 + Flags: flags, 62 + Action: func(c *cli.Context) error { 63 + return commandAction(c, action) 64 + }, 65 + } 66 + } 67 + 44 68 func main() { 45 69 homeFlags := []cli.Flag{ 46 70 &cli.BoolFlag{ 47 - Name: "force-home-manager", 71 + Name: "use-home-manager", 48 72 }, 49 73 &cli.StringFlag{ 50 74 Name: "user", ··· 52 76 } 53 77 osFlags := []cli.Flag{ 54 78 &cli.BoolFlag{ 55 - Name: "force-nixos-rebuild", 79 + Name: "use-nixos-rebuild", 56 80 }, 57 81 &cli.StringFlag{ 58 82 Name: "hostname", ··· 111 135 { 112 136 Name: "home", 113 137 Subcommands: []*cli.Command{ 114 - { 115 - Name: "switch", 116 - Aliases: []string{"sw"}, 117 - Flags: homeFlags, 118 - Action: func(c *cli.Context) error { 119 - return home(c, Switch) 120 - }, 121 - }, 122 - { 123 - Name: "build", 124 - Flags: homeFlags, 125 - Action: func(c *cli.Context) error { 126 - return home(c, Build) 127 - }, 128 - }, 138 + subcommand(Switch, []string{"sw"}, homeFlags, home), 139 + subcommand(Build, []string{}, homeFlags, home), 140 + subcommand(Instantiate, []string{}, homeFlags, home), 141 + subcommand(Generations, []string{"gens"}, homeFlags, home), 142 + subcommand(Packages, []string{"pkgs"}, homeFlags, home), 129 143 { 130 144 Name: "news", 131 145 Flags: []cli.Flag{ ··· 154 168 { 155 169 Name: "os", 156 170 Subcommands: []*cli.Command{ 157 - { 158 - Name: "switch", 159 - Aliases: []string{"sw"}, 160 - Flags: osFlags, 161 - Action: func(c *cli.Context) error { 162 - return ruiOS(c, Switch) 163 - }, 164 - }, 165 - { 166 - Name: "build", 167 - Flags: osFlags, 168 - Action: func(c *cli.Context) error { 169 - return ruiOS(c, Build) 170 - }, 171 - }, 171 + subcommand(Switch, []string{"sw"}, osFlags, ruiOS), 172 + subcommand(Boot, []string{}, osFlags, ruiOS), 173 + subcommand(Test, []string{}, osFlags, ruiOS), 174 + subcommand(Build, []string{}, osFlags, ruiOS), 175 + subcommand(DryActivate, []string{"dry"}, osFlags, ruiOS), 176 + subcommand(BuildVM, []string{"vm"}, osFlags, ruiOS), 172 177 }, 173 178 }, 174 179 { ··· 224 229 } 225 230 226 231 func actionName(action int) string { 227 - if action == Build { 228 - return "build" 232 + name := "switch" 233 + 234 + switch action { 235 + case Boot: 236 + name = "boot" 237 + 238 + break 239 + 240 + case Test: 241 + name = "test" 242 + 243 + break 244 + 245 + case Build: 246 + name = "build" 247 + 248 + break 249 + 250 + case DryActivate: 251 + name = "dry-activate" 252 + 253 + break 254 + 255 + case BuildVM: 256 + name = "build-vm" 257 + 258 + break 259 + 260 + case Instantiate: 261 + name = "instantiate" 262 + 263 + break 264 + 265 + case Generations: 266 + name = "generations" 267 + 268 + break 269 + 270 + case Packages: 271 + name = "packages" 272 + 273 + break 229 274 } 230 275 231 - return "switch" 276 + return name 232 277 } 233 278 234 - func actionVerb(action int) string { 235 - if action == Build { 236 - return "built" 279 + func actionDetails(action int) (string, string, bool) { 280 + switch action { 281 + case Switch: 282 + return actionName(action), "switched", true 283 + 284 + case Boot: 285 + return actionName(action), "booted", false 286 + 287 + case Test: 288 + return actionName(action), "tested", false 289 + 290 + case Build: 291 + return actionName(action), "built", true 292 + 293 + case DryActivate: 294 + return actionName(action), "dry activated", false 295 + 296 + case BuildVM: 297 + return actionName(action), "VM built", false 298 + 299 + case Instantiate: 300 + return actionName(action), "instantiated", false 301 + 302 + case Generations: 303 + return actionName(action), "generations listed", false 304 + 305 + case Packages: 306 + return actionName(action), "packages shown", false 237 307 } 238 308 239 - return "switched" 309 + return "", "", false 240 310 } 241 311 242 312 func home(c *cli.Context, action int) error { 243 313 nh, err := exec.LookPath("nh") 244 314 extraArgs := c.Args().Slice() 245 - actionName := actionName(action) 315 + name, verb, usableWithNH := actionDetails(action) 246 316 247 - if err := notify("Queued home " + actionName); err != nil { 317 + if err := notify("Queued home " + name); err != nil { 248 318 return err 249 319 } 250 320 251 - if err == nil && !c.Bool("force-home-manager") { 252 - err = command(nh, append([]string{"home", actionName, "--"}, 321 + if err == nil && !c.Bool("use-home-manager") { 322 + if !usableWithNH { 323 + return fmt.Errorf("This command is not supported with nh. Use --use-home-manager to use Home Manager instead.") 324 + } 325 + 326 + err = command(nh, append([]string{"home", name, "--"}, 253 327 extraArgs...)...) 254 328 } else { 255 329 user := c.String("user") ··· 264 338 flake = os.Getenv("FLAKE") 265 339 } 266 340 267 - err = command("home-manager", append([]string{actionName, 341 + err = command("home-manager", append([]string{name, 268 342 "--flake", fmt.Sprintf("%s#%s", flake, user)}, 269 343 extraArgs...)...) 270 344 } 271 345 272 346 if err != nil { 273 - return notify(fmt.Sprintf("Failed to %s home: %s", actionName, err.Error())) 347 + return notify(fmt.Sprintf("Failed to %s home: %s", name, err.Error())) 274 348 } 275 349 276 - return notify("Home " + actionVerb(action)) 350 + return notify("Home " + verb) 277 351 } 278 352 279 353 func ruiOS(c *cli.Context, action int) error { 280 354 nh, err := exec.LookPath("nh") 281 - actionName := actionName(action) 355 + name, verb, usableWithNH := actionDetails(action) 282 356 283 - if err := notify("Queued OS " + actionName); err != nil { 357 + if err := notify("Queued OS " + name); err != nil { 284 358 return err 285 359 } 286 360 287 - if err == nil && !c.Bool("force-nixos-rebuild") { 288 - err = command(nh, "os", actionName) 361 + if err == nil && !c.Bool("use-nixos-rebuild") { 362 + if !usableWithNH { 363 + return fmt.Errorf("This command is not supported with nh. Use --use-nixos-rebuild to use nixos-rebuild instead.") 364 + } 365 + 366 + err = command(nh, "os", name) 289 367 } else { 290 368 escalator := "sudo" 291 369 ··· 309 387 flake = os.Getenv("FLAKE") 310 388 } 311 389 312 - err = command(escalator, "nixos-rebuild", actionName, "--flake", 390 + err = command(escalator, "nixos-rebuild", name, "--flake", 313 391 fmt.Sprintf("%s#%s", flake, hostname)) 314 392 } 315 393 316 394 if err != nil { 317 - return notify(fmt.Sprintf("Failed to %s OS: %s", actionName, err.Error())) 395 + return notify(fmt.Sprintf("Failed to %s OS: %s", name, err.Error())) 318 396 } 319 397 320 - return notify("OS " + actionVerb(action)) 398 + return notify("OS " + verb) 321 399 }