Skip to content

Commit

Permalink
Rewrite login shell register with go (#216)
Browse files Browse the repository at this point in the history
* Rewrite login shell register with go

* Remove outdated job from CI

The linters will be run in precommit and Nix CI
  • Loading branch information
kachick authored Jul 30, 2023
1 parent 585dc97 commit e515a42
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 49 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,6 @@ jobs:
# Keep same version as used in *.nix
dprint-version: '0.39.1'

shellscript:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
# * ubuntu-latest includes shellcheck, However it is bit old. This ensures to use fixed version.
# * Standardizing to nix is best, but nix is slow even if installing only tiny binary
- uses: jdxcode/rtx-action@v1
with:
# Keep same version as used in *.nix
tool_versions: |
shellcheck 0.9.0
shfmt 3.7.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Logging dependency versions
run: |
shellcheck --version
shfmt --version
- name: Run linters
run: |
shopt -s globstar
shellcheck ./**/*.bash
shfmt --diff ./**/*.bash
typos:
timeout-minutes: 15
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ script = [
"home-manager switch -f ./home/.config/home-manager/home.nix",
]

[tasks.enable_login_shells]
command = 'sudo'
args = [
'-E',
'dist/enable_nix_login_shells',
]

[tasks.ci]
dependencies = [
'default',
Expand Down
57 changes: 57 additions & 0 deletions cmd/enable_nix_login_shells/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

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

// Called should be `sudo -E ...`, See https://stackoverflow.com/a/40438345/1212807

// 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.Fatalln("$HOME is not found")
}
if homePath == "/root" {
log.Fatalln("used by root looks weird. You should run `sudo -E ...` instead of `sudo ...`")
}

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("failed - could you correctly run this with sudo? - %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 e515a42

Please sign in to comment.