Skip to content

Commit

Permalink
Implement WSL setup script with zsh and docker integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Aug 10, 2023
1 parent 032c753 commit c70c74c
Show file tree
Hide file tree
Showing 4 changed files with 4,506 additions and 21 deletions.
79 changes: 58 additions & 21 deletions cmd/enable_wsl_systemd/main.go → cmd/setup_wsl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,18 @@ package main

import (
"fmt"
"io"
"log"
"os"
"strings"

"golang.org/x/sys/unix"

"github.com/foxboron/sbctl"
)

// Exists for remember https://github.com/kachick/dotfiles/pull/264#discussion_r1289600371

// This script requires sudo execution

func main() {
// wsl.exe returns non English even in called on the VM https://github.com/microsoft/WSL/issues/9242
// And always having non ASCII, annoy to depend with the output :<
uname := unix.Utsname{}
err := unix.Uname(&uname)
if err != nil {
log.Fatalf("cannot get uname: %+v\n", err)
}
unameStr := ""
// So here, using uname, as I understand it is same as `uname -r`
for _, i8 := range uname.Release {
unameStr += string(rune(int(i8)))
}
if !strings.Contains(unameStr, "microsoft-standard-WSL2") {
log.Fatalf("Looks executed on non WSL systems: %s", unameStr)
}

func mustActivateSystemDOnWSL() {
const path = "/etc/wsl.conf"

const systemdEnablingEntry = `[boot]
Expand All @@ -47,7 +31,8 @@ systemd=true`
}

if strings.Contains(wslConfig, "systemd") {
log.Fatalf("Looks areleady exists the systemd config")
log.Println("Skip - enabling syetemd - Looks areleady exists the systemd config")
return
}

dirty := strings.Clone(wslConfig)
Expand All @@ -68,3 +53,55 @@ See https://learn.microsoft.com/ja-jp/windows/wsl/systemd for further detail
`)
}
}

// https://github.com/docker/for-win/issues/8336#issuecomment-718369597
func mustPersistDockerZshCompletions() {
const completionLoadablePath = "/usr/share/zsh/vendor-completions/_docker"
err := os.Remove(completionLoadablePath)
if err != nil {
log.Printf("Skip - No zsh completions for docker found, docker-desktop may not be executed: %+v\n", err)
}
// Can't make immutable symlink, so copy and make immutable here
// https://unix.stackexchange.com/questions/586430/how-to-make-a-symlink-read-only-chattr-i-location-symlink
target, err := os.Create(completionLoadablePath)
if err != nil {
log.Panicf("%+v\n", err)
}
defer target.Close()
integration, err := os.Open("dependencies/docker/zsh-vendor-completions.zsh")
if err != nil {
log.Panicf("%+v\n", err)
}
defer integration.Close()
_, err = io.Copy(target, integration)
if err != nil {
log.Panicf("%+v\n", err)
}

err = sbctl.SetAttr(target, sbctl.FS_IMMUTABLE_FL)
if err != nil {
log.Panicf("%+v\n", err)
}
}

// This script requires sudo execution
func main() {
// wsl.exe returns non English even in called on the VM https://github.com/microsoft/WSL/issues/9242
// And always having non ASCII, annoy to depend with the output :<
uname := unix.Utsname{}
err := unix.Uname(&uname)
if err != nil {
log.Fatalf("cannot get uname: %+v\n", err)
}
unameStr := ""
// So here, using uname, as I understand it is same as `uname -r`
for _, i8 := range uname.Release {
unameStr += string(rune(int(i8)))
}
if !strings.Contains(unameStr, "microsoft-standard-WSL2") {
log.Fatalf("Looks executed on non WSL systems: %s", unameStr)
}

mustActivateSystemDOnWSL()
mustPersistDockerZshCompletions()
}
Loading

0 comments on commit c70c74c

Please sign in to comment.