❄ 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): notifications

fuwn.net 11010b69 d289f20f

verified
+74 -22
+1
.gitignore
··· 1 1 rui 2 2 .pre-commit-config.yaml 3 3 result 4 + config.json
+73 -22
rui.go
··· 1 1 package main 2 2 3 3 import ( 4 + "encoding/json" 4 5 "fmt" 5 6 "os" 6 7 "os/exec" ··· 67 68 _, err := exec.LookPath("nh") 68 69 extraArgs := []string{} 69 70 71 + if err := Notify("Queued home switch"); err != nil { 72 + return err 73 + } 74 + 70 75 if c.Bool("impure") { 71 76 extraArgs = []string{"--impure"} 72 77 } 73 78 74 79 if err == nil && !c.Bool("force-home-manager") { 75 - return Command("nh", append([]string{"home", "switch", "--"}, 80 + err = Command("nh", append([]string{"home", "switch", "--"}, 76 81 extraArgs...)...) 77 - } 82 + } else { 83 + user := c.String("user") 78 84 79 - user := c.String("user") 85 + if user == "" { 86 + user = os.Getenv("USER") 87 + } 80 88 81 - if user == "" { 82 - user = os.Getenv("USER") 89 + err = Command("home-manager", append([]string{"switch", 90 + "--flake", fmt.Sprintf("%s#%s", os.Getenv("FLAKE"), user)}, 91 + extraArgs...)...) 83 92 } 84 93 85 - return Command("home-manager", append([]string{"switch", 86 - "--flake", fmt.Sprintf("%s#%s", os.Getenv("FLAKE"), user)}, 87 - extraArgs...)...) 94 + if err != nil { 95 + return Notify(fmt.Sprintf("Failed to switch home: %s", err.Error())) 96 + } 97 + 98 + return Notify("Home switched") 88 99 }, 89 100 }, 90 101 { ··· 133 144 Action: func(c *cli.Context) error { 134 145 _, err := exec.LookPath("nh") 135 146 136 - if err == nil && !c.Bool("force-nixos-rebuild") { 137 - return Command("nh", "os", "switch") 147 + if err := Notify("Queued OS switch"); err != nil { 148 + return err 138 149 } 139 150 140 - _, err = exec.LookPath("doas") 141 - escalator := "sudo" 151 + if err == nil && !c.Bool("force-nixos-rebuild") { 152 + err = Command("nh", "os", "switch") 153 + } else { 154 + _, err = exec.LookPath("doas") 155 + escalator := "sudo" 142 156 143 - if err == nil { 144 - escalator = "doas" 145 - } 157 + if err == nil { 158 + escalator = "doas" 159 + } 146 160 147 - hostname := c.String("hostname") 161 + hostname := c.String("hostname") 148 162 149 - if hostname == "" { 150 - hostname, err = os.Hostname() 163 + if hostname == "" { 164 + hostname, err = os.Hostname() 151 165 152 - if err != nil { 153 - return err 166 + if err != nil { 167 + return err 168 + } 154 169 } 170 + 171 + err = Command(escalator, "nixos-rebuild", "switch", "--flake", 172 + fmt.Sprintf("%s#%s", os.Getenv("FLAKE"), hostname)) 155 173 } 156 174 157 - return Command(escalator, "nixos-rebuild", "switch", "--flake", 158 - fmt.Sprintf("%s#%s", os.Getenv("FLAKE"), hostname)) 175 + if err != nil { 176 + return Notify(fmt.Sprintf("Failed to switch OS: %s", err.Error())) 177 + } 178 + 179 + return Notify("OS switched") 159 180 }, 160 181 }, 161 182 }, ··· 184 205 185 206 return cmd.Run() 186 207 } 208 + 209 + type Configuration struct { 210 + Notify bool `json:"notify"` 211 + } 212 + 213 + func Notify(message string) error { 214 + content, err := os.ReadFile(os.Getenv("XDG_CONFIG_HOME")) 215 + 216 + if err != nil { 217 + return err 218 + } 219 + 220 + var configuration Configuration 221 + 222 + if err := json.Unmarshal(content, &configuration); err != nil { 223 + return err 224 + } 225 + 226 + notifySend, err := exec.LookPath("notify-send") 227 + 228 + if err != nil { 229 + return nil 230 + } 231 + 232 + if configuration.Notify { 233 + return Command(notifySend, "Rui", message) 234 + } 235 + 236 + return nil 237 + }