#!/usr/bin/env v import os // install.vsh sets vpac's config up for a new user: it asks a few questions, // then writes /etc/vpac.d/config.lip and the mirror file the config requires, // /etc/vpac.d/mirrors/.lip. 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/.lip 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 } fn distros() []Distro { return [ Distro{ name: 'arch' repos: ['core', 'extra'] src: 'https://geo.mirror.pkgbuild.com/\$repo/os/\$arch' }, Distro{ name: 'artix' repos: ['system', 'world', 'galaxy'] src: 'https://mirror1.artixlinux.org/repos/\$repo/os/\$arch' }, Distro{ name: 'cachyos' repos: ['cachyos'] src: 'https://mirror.cachyos.org/repo/\$arch/\$repo' }, ] } // 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 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.lip') mut replace := false if os.exists(config) { println('${config} exists.') keep := ask_yes('replace it? the old file is kept as config.lip.old', false)! if !keep { println('kept ${config}: nothing changed') return } replace = true } mut paths := own_paths() if found('pacman') { if ask_yes("pacman is installed: keep vpac's db, cache, keys, hooks and log in pacman's directories?", true)! { paths = pacman_paths() } else { println("note: vpac's own db starts empty and does not see the packages pacman installed") } } else { println("pacman is not installed: vpac links libalpm, which the pacman package ships, so install pacman before vpac can run. the config uses vpac's own directories") } distro := ask_choice('mirrors for which os', distros().map(it.name), detect_distro())! aur := ask_yes('add the AUR as a pkgbuild repo', true)! 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')! sandbox := ask_yes("turn libalpm's sandbox off for downloads and extraction (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 root: root parallel: parallel color: color sandbox: sandbox arch: os.uname().machine } d := distro_named(distro) mirror := os.join_path(etc, 'mirrors', '${d.name}.lip') write_mirror := !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.lip') staged_mirror := os.join_path(stage, 'mirrors', '${d.name}.lip') os.write_file(staged_config, config_text(a, paths, d))! os.write_file(staged_mirror, mirror_text(d))! mut cmds := ['mkdir -p ${sh_quote(etc)} ${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 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 { println('kept ${mirror}: it is already there') } println('') println('next:') println(term.bold(' v . -o vpac ') + ' build vpac') println(term.bold(' ln -sf $PWD/vpac /usr/local/bin') + ' symlink vpac to /usr/local/bin/vpac') 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 } // config_text is the one config file: every form a user is likely to want, // with the mirror file of the distro required beside it fn config_text(a Answers, p Paths, d Distro) string { mut repos := []string{} for name in d.repos { repos << '(repo\n :name "${name}"\n :src mirrors-${d.name})' } if a.aur { repos << '(repo\n :name "aur"\n :type "pkgbuild"\n :src "https://aur.archlinux.org")' } return '; vpac config, written by install.vsh: edit it by hand, or run install.vsh again. (require "mirrors/${d.name}.lip") (path :root "/" :db "${p.db}" :log "${p.log}" :gpg "${p.gpg}" :cache ["${p.cache}"] :hook ["${p.hook}"]) (pkg :arch "${a.arch}" :hold ["pacman" "glibc"]) (network :parallel ${a.parallel} :protocols ["https"] :user "alpm"' + if a.sandbox if { ':disable-sandbox true' } else { '' } + ' (options :root "${a.root}" :color "${a.color}") ${repos.join('\n\n')} ' } // mirror_text is a mirror file as rank writes one: mirrors- is defined // and holds the urls, here the distro's one url until the ranking replaces it fn mirror_text(d Distro) string { return '; mirrors for ${d.name}, the default until vpac k -w ranks them (unless (defined? mirrors-${d.name}) (define mirrors-${d.name} [])) (array-append! mirrors-${d.name} ["${d.src}"]) ' }