Skip to content

Commit

Permalink
Rewrite login shell register with go
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Jul 30, 2023
1 parent 585dc97 commit 4088266
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
52 changes: 52 additions & 0 deletions cmd/enable_nix_login_shells/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"log"
"os"
"strings"
)

// This script requires sudo execution, if it is a reasonable way, including in home.nix may be better

func main() {
homePath, ok := os.LookupEnv("HOME")
if !ok {
log.Fatalf("$HOME is not found")
}

loginAbles := []string{"zsh", "fish"}

etcShellsBytes, err := os.ReadFile("/etc/shells")
if err != nil {
log.Fatalf("%v\n", err)
}

etcShells := string(etcShellsBytes)
dirty := strings.Clone(etcShells)
examplePath := ""

for _, sh := range loginAbles {
shellPath := homePath + "/.nix-profile/bin/" + sh
if strings.Contains(etcShells, shellPath) {
log.Printf("skip - %s is already registered in /etc/shells\n", shellPath)
} else {
log.Printf("insert - %s will be registered in /etc/shells\n", shellPath)
examplePath = shellPath
dirty += fmt.Sprintln(shellPath)
}
}

if dirty != etcShells {
err = os.WriteFile("/etc/shells", []byte(dirty), os.ModePerm)
if err != nil {
log.Fatalf("%v\n", err)
}

fmt.Printf(`
Done! Set one of your favorite shell as follows
chsh -s %s "$(whoami)"
`, examplePath)
}
}
24 changes: 0 additions & 24 deletions scripts/register_login_shells.bash

This file was deleted.

0 comments on commit 4088266

Please sign in to comment.