| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 |
|
| 22 | (define-module (gnu system images wsl2)
|
| 23 | #:use-module (gnu bootloader)
|
| 24 | #:use-module (gnu image)
|
| 25 | #:use-module (gnu packages admin)
|
| 26 | #:use-module (gnu packages base)
|
| 27 | #:use-module (gnu packages bash)
|
| 28 | #:use-module (gnu packages guile)
|
| 29 | #:use-module (gnu packages linux)
|
| 30 | #:use-module (gnu services)
|
| 31 | #:use-module (gnu services base)
|
| 32 | #:use-module (gnu system)
|
| 33 | #:use-module (gnu system image)
|
| 34 | #:use-module (gnu system shadow)
|
| 35 | #:use-module (guix build-system trivial)
|
| 36 | #:use-module (guix gexp)
|
| 37 | #:use-module (guix packages)
|
| 38 | #:use-module ((guix licenses) #:select (fsdg-compatible))
|
| 39 | #:export (wsl-boot-program
|
| 40 | make-wsl-os
|
| 41 | make-wsl-image
|
| 42 | wsl2-image))
|
| 43 |
|
| 44 | (define (wsl-boot-program user)
|
| 45 | "Program that runs the system boot script, then starts a login shell as
|
| 46 | USER."
|
| 47 | (program-file
|
| 48 | "wsl-boot-program"
|
| 49 | (with-imported-modules '((guix build syscalls))
|
| 50 | #~(begin
|
| 51 | (use-modules (guix build syscalls))
|
| 52 | (unless (file-exists? "/run/current-system")
|
| 53 | (let ((shepherd-socket "/var/run/shepherd/socket"))
|
| 54 | |
| 55 | (when (file-exists? shepherd-socket)
|
| 56 | (delete-file shepherd-socket))
|
| 57 |
|
| 58 | |
| 59 | (when (zero? (primitive-fork))
|
| 60 | (let* ((system-generation
|
| 61 | (readlink "/var/guix/profiles/system"))
|
| 62 | (system (readlink
|
| 63 | (string-append
|
| 64 | (if (absolute-file-name? system-generation)
|
| 65 | ""
|
| 66 | "/var/guix/profiles/")
|
| 67 | system-generation))))
|
| 68 | (setenv "GUIX_NEW_SYSTEM" system)
|
| 69 | (execl #$(file-append guile-3.0 "/bin/guile")
|
| 70 | "guile"
|
| 71 | "--no-auto-compile"
|
| 72 | (string-append system "/boot"))))
|
| 73 |
|
| 74 | |
| 75 | (while (not (file-exists? shepherd-socket))
|
| 76 | (sleep 1))))
|
| 77 |
|
| 78 | (let* ((pw (getpw #$user))
|
| 79 | (shell (passwd:shell pw))
|
| 80 | (sudo #+(file-append sudo "/bin/sudo"))
|
| 81 | (args (cdr (command-line)))
|
| 82 | (uid (passwd:uid pw))
|
| 83 | (gid (passwd:gid pw))
|
| 84 | (runtime-dir (string-append "/run/user/"
|
| 85 | (number->string uid))))
|
| 86 | |
| 87 | |
| 88 | (setenv "WSLPATH" (getenv "PATH"))
|
| 89 |
|
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | (mount #f "/run" #f
|
| 95 | MS_REMOUNT
|
| 96 | #:update-mtab? #f)
|
| 97 |
|
| 98 | |
| 99 | (unless (file-exists? runtime-dir)
|
| 100 | (mkdir runtime-dir)
|
| 101 | (chown runtime-dir uid gid))
|
| 102 | (setenv "XDG_RUNTIME_DIR" runtime-dir)
|
| 103 |
|
| 104 | |
| 105 | (apply execl sudo "sudo"
|
| 106 | "--preserve-env=WSLPATH,XDG_RUNTIME_DIR"
|
| 107 | "-u" #$user
|
| 108 | "--"
|
| 109 | shell "-l" args))))))
|
| 110 |
|
| 111 | (define dummy-package
|
| 112 | (package
|
| 113 | (name "dummy")
|
| 114 | (version "0")
|
| 115 | (source #f)
|
| 116 | (build-system trivial-build-system)
|
| 117 | (arguments
|
| 118 | `(#:modules ((guix build utils))
|
| 119 | #:target #f
|
| 120 | #:builder (begin
|
| 121 | (use-modules (guix build utils))
|
| 122 | (let* ((out (assoc-ref %outputs "out"))
|
| 123 | (dummy (string-append out "/dummy")))
|
| 124 | (mkdir-p out)
|
| 125 | (call-with-output-file dummy
|
| 126 | (const #t))))))
|
| 127 | (home-page #f)
|
| 128 | (synopsis #f)
|
| 129 | (description #f)
|
| 130 | (license (fsdg-compatible "dummy"))))
|
| 131 |
|
| 132 | (define dummy-bootloader
|
| 133 | (bootloader
|
| 134 | (name 'dummy-bootloader)
|
| 135 | (package dummy-package)
|
| 136 | (configuration-file "/var/lib/wsl-bootloader.cfg")
|
| 137 | (configuration-file-generator
|
| 138 | (lambda (. _rest)
|
| 139 | (computed-file "dummy-bootloader"
|
| 140 | #~(call-with-output-file #$output
|
| 141 | (lambda (port) (display "" port))))))
|
| 142 | (installer #~(const #t))))
|
| 143 |
|
| 144 | (define dummy-kernel dummy-package)
|
| 145 |
|
| 146 | (define (dummy-initrd . _rest)
|
| 147 | (plain-file "dummy-initrd" ""))
|
| 148 |
|
| 149 | (define* (wsl-conf-file #:key
|
| 150 | (default-user "guest")
|
| 151 | (hostname "gnu")
|
| 152 | (automount? #t)
|
| 153 | (mount-root "/mnt")
|
| 154 | (mount-options "metadata,umask=22,fmask=11")
|
| 155 | (generate-hosts? #t)
|
| 156 | (generate-resolv? #t)
|
| 157 | (interop? #t)
|
| 158 | (append-windows-path? #t)
|
| 159 | (systemd? #f))
|
| 160 | "Return a plain-file containing the content for /etc/wsl.conf.
|
| 161 | This configuration file is read by the WSL kernel when starting the
|
| 162 | distribution. It controls the default user, systemd mode, automount
|
| 163 | settings, network behavior, and interop features."
|
| 164 | (plain-file
|
| 165 | "wsl.conf"
|
| 166 | (string-append
|
| 167 | "[user]\n"
|
| 168 | "default=" default-user "\n\n"
|
| 169 | "[boot]\n"
|
| 170 | "systemd=" (if systemd? "true" "false") "\n\n"
|
| 171 | "[automount]\n"
|
| 172 | "enabled=" (if automount? "true" "false") "\n"
|
| 173 | "root=" mount-root "\n"
|
| 174 | "options=" mount-options "\n\n"
|
| 175 | "[network]\n"
|
| 176 | "hostname=" hostname "\n"
|
| 177 | "generateHosts=" (if generate-hosts? "true" "false") "\n"
|
| 178 | "generateResolvConf=" (if generate-resolv? "true" "false") "\n\n"
|
| 179 | "[interop]\n"
|
| 180 | "enabled=" (if interop? "true" "false") "\n"
|
| 181 | "appendWindowsPath=" (if append-windows-path? "true" "false") "\n")))
|
| 182 |
|
| 183 | (define* (wsl-distribution-conf #:key
|
| 184 | (name "guix")
|
| 185 | (default-uid 1000)
|
| 186 | (oobe-command #f)
|
| 187 | (icon #f)
|
| 188 | (terminal? #t))
|
| 189 | "Return a plain-file containing the content for /etc/wsl-distribution.conf.
|
| 190 | This file consumed by the WSL installer (e.g., when double-clicking a .wsl
|
| 191 | file or using 'wsl --install --from-file') to determine the distribution name,
|
| 192 | default UID, OOBE command, shortcut settings, and Windows Terminal integration."
|
| 193 | (plain-file
|
| 194 | "wsl-distribution.conf"
|
| 195 | (string-append
|
| 196 | "[oobe]\n"
|
| 197 | (if oobe-command
|
| 198 | (string-append "command=" oobe-command "\n")
|
| 199 | "")
|
| 200 | "defaultUid=" (number->string default-uid) "\n"
|
| 201 | "defaultName=" name "\n\n"
|
| 202 | "[shortcut]\n"
|
| 203 | "enabled=true\n"
|
| 204 | (if icon (string-append "icon=" icon "\n") "")
|
| 205 | "\n[windowsterminal]\n"
|
| 206 | "enabled=" (if terminal? "true" "false") "\n")))
|
| 207 |
|
| 208 | (define* (make-wsl-os #:key
|
| 209 | (user "guest")
|
| 210 | (uid 1000)
|
| 211 | (hostname "gnu")
|
| 212 | (extra-conf '()))
|
| 213 | "Return an operating system suitable for use as WSL2 distribution.
|
| 214 | USER, UID, and HOSTNAME are the default user account name, its UID, and the
|
| 215 | system host name. EXTRA-CONF is an optional list of additional files to be
|
| 216 | placed under /etc; each element is a pair (FILE-NAME . FILE-LIKE), where
|
| 217 | FILE-NAME is a string and FILE-LIKE is a file-like object (e.g., a plain-file
|
| 218 | or local-file). These files will be installed alongside wsl.conf and
|
| 219 | wsl-distribution.conf in the generated image."
|
| 220 | (operating-system
|
| 221 | (host-name hostname)
|
| 222 | (timezone "Etc/UTC")
|
| 223 | (bootloader (bootloader-configuration
|
| 224 | (bootloader dummy-bootloader)))
|
| 225 | (kernel dummy-kernel)
|
| 226 | (initrd dummy-initrd)
|
| 227 | (initrd-modules '())
|
| 228 | (firmware '())
|
| 229 | (file-systems '())
|
| 230 | (users (cons* (user-account
|
| 231 | (name user)
|
| 232 | (uid uid)
|
| 233 | (group "users")
|
| 234 | (supplementary-groups '("wheel"))
|
| 235 | (password "")
|
| 236 | (comment "WSL user"))
|
| 237 | (user-account
|
| 238 | (inherit %root-account)
|
| 239 | (shell (wsl-boot-program user)))
|
| 240 | %base-user-accounts))
|
| 241 | (services
|
| 242 | (list
|
| 243 | (service guix-service-type)
|
| 244 | (simple-service 'wsl-meta etc-service-type
|
| 245 | `(("wsl.conf" ,(wsl-conf-file #:default-user user
|
| 246 | #:hostname (operating-system-host-name this-operating-system)))
|
| 247 | ("wsl-distribution.conf" ,(wsl-distribution-conf #:name "guix"
|
| 248 | #:default-uid uid))
|
| 249 | ,@extra-conf))
|
| 250 | (service special-files-service-type
|
| 251 | `(("/bin/sh" ,(file-append bash "/bin/bash"))
|
| 252 | ("/bin/mount" ,(file-append util-linux "/bin/mount"))
|
| 253 | ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))))
|
| 254 |
|
| 255 | (define* (make-wsl-image #:key
|
| 256 | (user "guest")
|
| 257 | (uid 1000)
|
| 258 | (hostname "gnu")
|
| 259 | (extra-conf '()))
|
| 260 | "Return an image record for a WSL2 distribution.
|
| 261 | All keyword arguments are passed to 'make-wsl-os'."
|
| 262 | (image
|
| 263 | (inherit
|
| 264 | (os->image (make-wsl-os #:user user
|
| 265 | #:uid uid
|
| 266 | #:hostname hostname
|
| 267 | #:extra-conf extra-conf)
|
| 268 | #:type wsl2-image-type))
|
| 269 | (name 'wsl-image)))
|
| 270 |
|
| 271 | (define wsl2-image
|
| 272 | (make-wsl-image))
|
| 273 |
|
| 274 | wsl2-image
|