#!/usr/bin/env v import os import term // install.vsh sets vpac's config up for a new user: it asks a few questions, // then writes /etc/vpac.d/config.vile and the mirror file the config requires, // /etc/vpac.d/mirrors/.vile. the config stays in /etc/vpac.d whatever the // answers: the pacman choice only picks where the db, cache, keys, hooks and // log live. VPAC_ETC writes the two files into another directory instead, a // trial that needs no root and makes no directory outside it. the binary is // built with `v . -o vpac`, which the installer's next steps name. const default_etc = '/etc/vpac.d' // distro is an os the config is written for: its name, which is also the rank // name (mirrors/.vile defines mirrors-), its repos, and the url its // mirrors start from, with the $repo and $arch placeholders left in it struct Distro { name string repos []string src string // lib32 is the distro's repo of 32-bit libraries, '' when it ships none: // the shared mirror list serves it, the url's $repo picks the repo. // cachyos builds a monorepo, so it has none and its question is skipped lib32 string } fn distros() []Distro { return [ Distro{ name: 'arch' repos: ['core', 'extra'] src: 'https://geo.mirror.pkgbuild.com/\$repo/os/\$arch' lib32: 'multilib' }, Distro{ name: 'artix' repos: ['system', 'world', 'galaxy'] src: 'https://mirror1.artixlinux.org/repos/\$repo/os/\$arch' lib32: 'lib32' }, Distro{ name: 'cachyos' repos: ['cachyos'] src: 'https://mirror.cachyos.org/repo/\$arch/\$repo' lib32: '' }, Distro{ name: 'vinix' }, ] } // paths are where vpac keeps its db, cache, keyring and hooks, and its log struct Paths { db string cache string gpg string hook string log string } // pacman_paths are the directories pacman itself uses, which libalpm defaults // to. the local db is pacman's own, so vpac sees the packages pacman installed fn pacman_paths() Paths { return Paths{ db: '/var/lib/pacman/' cache: '/var/cache/pacman/pkg/' gpg: '/etc/pacman.d/gnupg/' hook: '/etc/pacman.d/hooks/' log: '/var/log/pacman.log' } } // own_paths are vpac's own, apart from pacman's. the db starts empty: it does // not know the packages pacman installed, and the keyring has no keys until // they are imported into it fn own_paths() Paths { return Paths{ db: '/var/lib/vpac/' cache: '/var/cache/vpac/pkg/' gpg: '/etc/vpac.d/gnupg/' hook: '/etc/vpac.d/hooks/' log: '/var/log/vpac.log' } } // answers are what the questions settled struct Answers { distro string aur bool lib32 bool root string parallel int color string sandbox bool arch string } fn main() { run() or { eprintln('install: ${err}') exit(1) } } fn run() ! { trial := os.getenv('VPAC_ETC') != '' etc := if trial { os.getenv('VPAC_ETC') } else { default_etc } config := os.join_path(etc, 'config.vile') mut replace := false if os.exists(config) { println('${config} exists.') keep := ask_yes('replace it? the old file is kept as config.vile.old', false)! if !keep { println('kept ${config}: nothing changed') return } replace = true } detected := detect_distro() share_pacman := detected != 'vinix' && found('pacman') && ask_yes("pacman is installed: keep vpac's db, cache, keys, hooks and log in pacman's directories?", true)! distro := ask_choice('configure vpac for which os', distros().map(it.name), detected)! d := distro_named(distro) vinix := distro == 'vinix' mut paths := own_paths() if vinix { paths = Paths{ ...paths db: '/var/lib/vpac/db/' } println('Vinix uses /var/lib/vpac/root for payloads and /var/lib/vpac/db for its database.') } else if share_pacman { paths = pacman_paths() } else { println("using vpac's own directories and package database") } aur := !vinix && ask_yes('add the AUR as a pkgbuild repo', true)! mut lib32 := false if d.lib32 != '' { // pacman.conf ships multilib commented out, so off is the default here too lib32 = ask_yes('add the ${d.lib32} repo for 32-bit libraries', false)! } providers := ['doas', 'sudo', 'pkexec', 'su'] root := ask_choice('the command vpac gets root with', providers, root_default())! parallel := ask_count('parallel downloads', 3, 1)! color := ask_choice('color output', ['auto', 'always', 'never'], 'auto')! if vinix { println('Vinix lacks Landlock. Choose yes below to fetch packages and repository databases with the privileges of the vpac process.') } sandbox := ask_yes("turn off nspkg's download sandbox (pacman's DisableSandbox)", false)! // the root command runs when the files are written to /etc: the same one // the config names for vpac's own escalation escalate := !trial && os.getuid() != 0 if escalate && !found(root) { return error('${root} is not installed: install it, or run install.vsh as root') } a := Answers{ distro: distro aur: aur lib32: lib32 root: root parallel: parallel color: color sandbox: sandbox arch: os.uname().machine } mirror := os.join_path(etc, 'mirrors', '${d.name}.vile') write_mirror := d.src != '' && !os.exists(mirror) // the files are staged in a temp dir and then moved into place, so the // root command only copies stage := os.join_path(os.temp_dir(), 'vpac-install-${os.getpid()}') os.mkdir_all(os.join_path(stage, 'mirrors'))! defer { os.rmdir_all(stage) or { eprintln('install: could not remove ${stage}: ${err}') } } staged_config := os.join_path(stage, 'config.vile') staged_mirror := os.join_path(stage, 'mirrors', '${d.name}.vile') os.write_file(staged_config, config_text(a, paths, d))! if write_mirror { os.write_file(staged_mirror, mirror_text(d))! } mut cmds := ['mkdir -p ${sh_quote(etc)}'] if write_mirror { cmds << 'mkdir -p ${sh_quote(os.join_path(etc, 'mirrors'))}' } if !trial { cmds << 'mkdir -p ${sh_quote(paths.db)} ${sh_quote(paths.cache)} ${sh_quote(paths.gpg)} ${sh_quote(paths.hook)}' if vinix { cmds << 'mkdir -p /var/lib/vpac/root' } } if replace { cmds << 'cp -f ${sh_quote(config)} ${sh_quote(config + '.old')}' } cmds << 'install -m 644 ${sh_quote(staged_config)} ${sh_quote(config)}' if write_mirror { cmds << 'install -m 644 ${sh_quote(staged_mirror)} ${sh_quote(mirror)}' } line := cmds.join(' && ') argv := if escalate { root_argv(root, line) } else { ['sh', '-c', line] } code := os.system_args(argv) if code != 0 { return error('writing ${config} failed (exit ${code}): fix the cause and run install.vsh again') } println('wrote ${config}') if write_mirror { println('wrote ${mirror}: one default url, which vpac k -w ranks') } else if d.src != '' { println('kept ${mirror}: it is already there') } println('') println('next:') build := if vinix { 'v -prod -cc gcc -o vpac .' } else { 'v . -o vpac' } println(term.bold(' ${build}') + ' build vpac') println(term.bold(' ln -sf $PWD/vpac /usr/local/bin') + ' symlink vpac to /usr/local/bin/vpac') if vinix { println(' Import and trust your package signing key in ${paths.gpg} before installing a signed local package.') } else { println(term.bold(' vpac k -w ') + ' rank the ${d.name} mirrors into ${mirror}') } } // distro_named is the distro with the name, or the first one when none has it fn distro_named(name string) Distro { for d in distros() { if d.name == name { return d } } return distros()[0] } // detect_distro reads /etc/os-release the way the ranker does: the ID, then the // ID_LIKE, the first name that is one of the distros fn detect_distro() string { text := os.read_file('/etc/os-release') or { return 'arch' } mut id := '' mut like := '' for line in text.split_into_lines() { if line.starts_with('ID=') { id = line[3..].trim('"\'') } else if line.starts_with('ID_LIKE=') { like = line[8..].trim('"\'') } } for candidate in [id, ...like.split(' ')] { for d in distros() { if d.name == candidate { return candidate } } } return 'arch' } // found reports whether an executable is on the PATH fn found(exe string) bool { path := os.find_abs_path_of_executable(exe) or { '' } return path != '' } // root_default is the first root command on the machine, in the order the // installer prefers them fn root_default() string { for p in ['doas', 'sudo', 'pkexec', 'su'] { if found(p) { return p } } return 'su' } // root_argv is the command that runs one shell line as root through the root // command. su takes the line with -c, the others run it through sh fn root_argv(provider string, line string) []string { if provider == 'su' { return ['su', '-c', line] } return [provider, 'sh', '-c', line] } // sh_quote spells one word for a posix shell, in single quotes fn sh_quote(s string) string { return "'" + s.replace("'", "'\\''") + "'" } // read_answer reads one line off stdin. the end of input is an error and not an // answer, so a run with stdin closed stops before it writes anything fn read_answer(prompt string) !string { line := os.input_opt(prompt) or { return error('stdin closed before the questions were answered: nothing was written') } return line.trim_space() } // ask reads one answer: the default when it is left empty fn ask(question string, default string) !string { suffix := if default == '' { '' } else { ' [${default}]' } answer := read_answer('${question}${suffix}: ')! return if answer == '' { default } else { answer } } fn ask_yes(question string, default bool) !bool { hint := if default { 'Y/n' } else { 'y/N' } for { raw := read_answer('${question} [${hint}]: ')! match raw.to_lower() { '' { return default } 'y', 'yes' { return true } 'n', 'no' { return false } else { println('answer y or n') } } } return default } fn ask_choice(question string, choices []string, default string) !string { for { answer := ask('${question} (${choices.join(' / ')})', default)! if answer in choices { return answer } println('answer one of: ${choices.join(', ')}') } return default } fn ask_count(question string, default int, min int) !int { for { answer := ask(question, '${default}')! if answer.len > 0 && answer.bytes().all(it.is_digit()) && answer.int() >= min { return answer.int() } println('answer a whole number, ${min} or more') } return default } // The mirror file supplies the servers shared by the distro's repositories. fn config_text(a Answers, p Paths, d Distro) string { mut repos := []string{} for name in d.repos { repos << 'sync-repo("${name}", mirrors-${d.name})' } if a.lib32 { repos << 'sync-repo("${d.lib32}", mirrors-${d.name})' } if a.aur { repos << 'pkgbuild-repo("aur", "https://aur.archlinux.org")' } vinix := d.name == 'vinix' mut lines := ['; vpac configuration'] if d.src != '' { lines << 'load("mirrors/${d.name}.vile")' } root := if vinix { '/var/lib/vpac/root/' } else { '/' } hold := if vinix { '[]' } else { '["pacman", "glibc"]' } user := if vinix { 'nobody' } else { 'alpm' } lines << ['', 'path-root = "${root}"', 'path-db = "${p.db}"', 'path-log = "${p.log}"', 'path-gpg = "${p.gpg}"', 'path-cache = ["${p.cache}"]', 'path-hook = ["${p.hook}"]', '', 'pkg-arch = "${a.arch}"', 'pkg-hold = ${hold}', '', 'network-connections = ${a.parallel}', 'network-protocols = ["https"]', 'network-user = "${user}"'] if a.sandbox { lines << 'network-disable-sandbox = true' } if vinix { lines << 'network-siglevel = ["package", "database"]' lines << r'pkgbuild-makepkg = "PACMAN=/opt/vpac/fakeroot/bin/vpac-query PATH=/opt/vpac/fakeroot/bin:$PATH makepkg -f --sign"' } lines << ['', 'options-root = "${a.root}"', 'options-color = "${a.color}"', ''] if repos.len > 0 { lines << 'repositories = list(' + '\n ' + repos.join(',\n ') + '\n)' } return lines.join('\n') + '\n' } fn mirror_text(d Distro) string { return '; mirrors for ${d.name}, the default until vpac k -w ranks them\nmirrors-${d.name} := ["${d.src}"]\n' }