diff --git a/cmd/enable_nix_login_shells/main.go b/cmd/enable_nix_login_shells/main.go new file mode 100644 index 00000000..77657cc6 --- /dev/null +++ b/cmd/enable_nix_login_shells/main.go @@ -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) + } +} diff --git a/scripts/register_login_shells.bash b/scripts/register_login_shells.bash deleted file mode 100755 index 3304fb01..00000000 --- a/scripts/register_login_shells.bash +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash - -# -x shows trace logs -set -euo pipefail - -register_login_shells() { - local -r zsh_path="$(which zsh)" - local -r fish_path="$(which fish)" - - # https://stackoverflow.com/a/3557165/1212807 - # https://stackoverflow.com/a/49049781/1212807 - ( - grep -qxF "$zsh_path" /etc/shells || echo "$zsh_path" - grep -qxF "$fish_path" /etc/shells || echo "$fish_path" - ) | sudo tee -a /etc/shells -} - -register_login_shells - -cat <<'EOF' - Done! Set one of your favorite as below - - chsh -s "$(which fish)" "$(whoami)" -EOF