repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

package.nu: can isntall packages now

c594fb051e840ecd06191276f0fbeacb99d9d4dc

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (11)

nushell/gen-aliases.nu

Modified
17 17 # Start defining your aliases here
19 19 # semantic aliases
20 -aka tree tre '-all' '--linecount' '--symbolic'
20 +aka tree tre '--all' '--symbolic'
21 21 aka view wezterm imgcat
22 22 aka list ls '--long' '--all' '--du'
23 23 aka show bat

nushell/modules/package/db.nu

Deleted
deleted file mode 100644

No text changes.

nushell/modules/package/extractor.nu

Modified
1 +# extracts various types of archives
3 +export def unpack [
4 + archive: path
5 + target: path
6 +] {
7 + # FIXME: maybe it is unnecessary to give type?
8 + # let extension = $archive | path parse | get extension
9 + # let type = match $extension {
10 + # 'gz' => 'gzip'
11 + # 'bz' => 'bzip2'
12 + # 'xz' => 'xz'
13 + # _ => { error make { msg: $"Unsupported extension type ($extension)!" } }
14 + # }
15 + # run-external tar '--extract' $"--($type)" $"--file=($archive)" $"--directory=($target)" '--strip-components=1'
17 + # TODO: test on win32
18 + let is_single_dir = (run-external tar '--list' $'--file=($archive)' | lines | path split | first | uniq | length) == 1
19 + let strip = if $is_single_dir { 1 } else { 0 }
20 + run-external tar '--extract' $"--file=($archive)" $"--directory=($target)" $'--strip-components=($strip)'

nushell/modules/package/folders.nu

Modified
2 2 # Defines, and creates, if they do not exist, all special paths, that
3 3 # `package.nu` uses to operate.
5 5 const package_home = $nu.home-path ++ '/Packages'
7 6 export def package-home [] {
8 7 mkdir $package_home
9 8 $package_home
12 11 const manifests = $package_home ++ '/Manifests'
14 12 export def manifests [] {
15 13 mkdir $manifests
16 14 $manifests
17 +const cache = $package_home ++ '/Cache'
18 +export def cache [] {
19 + mkdir $cache
20 + $cache
23 +const binaries = $package_home ++ '/Binaries'
24 +export def binaries [] {
25 + mkdir $binaries
26 + $binaries
29 +const database = $package_home ++ '/Database'
30 +export def database [] {
31 + mkdir $database
32 + $database
35 +const programs = $package_home ++ '/Programs'
36 +export def programs [] {
37 + mkdir $programs
38 + $programs
41 +const provisions = $package_home ++ '/Provisions'
42 +export def provisions [] {
43 + mkdir $provisions
44 + $provisions

nushell/modules/package/installer.nu

Added
new file mode 100644
1 +# Installs manifests.
2 +# Installign a manifest can mean to download the package and place the files
3 +# into special folders managed by `package.nu`, or delegating to the native package
4 +# manager.
5 +# The precedence of installation method is:
6 +# native package manager > pachages > language package manager
8 +use ./lib.nu *
9 +use ./extractor.nu
10 +use ./folders.nu
12 +export def install [
13 + query: string
14 +] {
15 + cd (folders manifests)
16 + let candidates = ls | where name =~ $"\(?i\)($query)" | get name
17 + if ($candidates | is-empty) {
18 + print --stderr $"No manifest for ($query) found!"
19 + return
20 + }
22 + mut package = {}
24 + if ($candidates | has-one) {
25 + let candidate = ($candidates | first)
26 + print $"Found only match for ($query): ($candidate)."
27 + $package = $candidate
28 + } else {
29 + print $"Found multiple matches for ($query). Pick one!"
30 + $package = ($candidates | input list --fuzzy "Which package to install? ")
31 + }
33 + let manifest = open $package | from nuon
34 + install-manifest $manifest
37 +export def install-manifest [
38 + manifest: record<
39 + organisation: string
40 + name: string
41 + source: string
42 + version: string
43 + 'version extractor': any
44 + 'relative binary path': path
45 + homepage: string
46 + description: string
47 + 'linux x64 url': any
48 + 'linux aarch64 url': any
49 + 'win32 x64 url': any
50 + 'fedora package name': any
51 + 'arch package name': any
52 + 'termux package name': any
53 + 'win32 package name': any
54 + >
55 +] {
56 + mut url = match ($nu.os-info.family) {
57 + 'unix' => {
58 + match ($nu.os-info.arch) {
59 + 'x86_64' => $manifest.'linux x64 url'
60 + 'aarch64' => $manifest.'linux aarch64 url'
61 + }
62 + }
63 + 'windows' => $manifest.'win32 x64 url'
64 + }
66 + if ($url | is-empty) {
67 + error make { msg: $"Manifest for ($manifest.name) coontains no suitable url for ($nu.os-info.name) ($nu.os-info.arch). Aborting." }
68 + }
70 + $url = ($url | str replace --regex --all '\{\s*version\s*\}' $manifest.version)
72 + let artifact_path = [ (folders cache) ($url | path basename) ] | path join
73 + if (not ($artifact_path | path exists)) {
74 + http get --raw --max-time 30sec $url | save --progress --raw $artifact_path
75 + }
77 + let destination = [(folders programs) $manifest.organisation $manifest.name $manifest.version] | path join
78 + mkdir $destination
79 + extractor unpack $artifact_path $destination
81 + let bin = $manifest.'relative binary path'
82 + if ($bin | is-empty) {
83 + print $"Manifest for ($manifest.organisation)/($manifest.name) contains to binary path."
84 + return
85 + }
86 + let source = [$destination $bin] | path join
87 + if ( not ($source | path exists)) {
88 + error make { msg: $"Faulty manifest! Relative binary path [($source)] points to non-existent path in ($destination)." }
89 + }
91 + match ($source | path type) {
92 + 'file' => {
93 + let target = ([(folders binaries) $bin] | path join)
94 + slink $source $target
95 + }
96 + 'directory' => {
97 + ls $source
98 + | where type == 'file'
99 + | get name
100 + | filter { is-executable }
101 + | par-each {
102 + |file|
103 + let target = ([(folders binaries) ($file | path basename)] | path join)
104 + slink ($file | path expand) $target
107 + _ => {
108 + error make { msg: $"Weird, ($source) is neither file nor directory..." }

nushell/modules/package/lib.nu

Modified
1 +# utility functions
2 +# TODO: move this into nushell modules
4 +# returs true, if the sequene has exactly one element
5 +export def has-one []: list<any> -> bool {
6 + let size = $in | length
7 + $size == 1
8 +}
10 +# Gets value of a column, or falls back to given alternative value.
11 +export def get-or-else [
12 + column: string # name of the column to get its value from
13 + value: any # alternative value to return, in case `get` gets nothing
14 +]: [
15 + record -> any
16 + table -> any
17 +] {
18 + select -i $column | update $column { if ( $in | is-empty ) { $value } else { $in } } | get $column
21 +# checks if a file is executable
22 +export def is-executable []: path -> bool {
23 + match $nu.os-info.family {
24 + 'unix' => {
25 + $env | upsert __file_for_executbale_check ( $in | path expand )
26 + run-external sh '-c' '[ -x "$__file_for_executbale_check" ] && echo true || echo false' | into bool
27 + }
28 + 'windows' => {
29 + # poor mans check on win32
30 + $in | find --regex '(\.exe)|(\.bat)|(\.ps1)$' | is-not-empty
31 + }
32 + }
35 +# creates a symbolic link
36 +export def slink [
37 + source: path # the original file path
38 + target: path # the file of the symbolic link
39 +] {
40 + match $nu.os-info.family {
41 + 'unix' => {
42 + run-external ln '--symbolic' $source $target
43 + }
44 + 'windows' => {
45 + # TODO: test on win32
46 + run-external cmd '/c' mklink $target $source
47 + }
48 + }

nushell/modules/package/mod.nu

Modified
9 9 # grabbed.
11 11 export module ./manifestor.nu
12 +export module ./installer.nu

nushell/modules/package/path.nu

Added
new file mode 100644
1 +# setup PATH envrionment for installed packages
3 +use ./folders.nu
5 +# sets PATH, so that it points to installed packages
6 +export def --env set-system-path [] {
7 + let bin = (folders binaries)
8 + match $nu.os-info.family {
9 + 'unix' => {
10 + if ( $env.PATH | find $bin | is-empty) {
11 + let envd = [$nu.home-dir .config environment.d] | path join
12 + if(not ($envd | path exists)) {
13 + error make {msg: $"systemd environment.d must exist under ($envd)!"}
14 + }
15 + let packgage_path = $'PATH = "$PATH:($bin)"'
16 + $packgage_path | save --raw ([$envd package.conf] | path join)
17 + $env | upsert PATH $bin
18 + }
19 + }
20 + 'windows' => {
21 + if ( $env.Path | find $bin | is-empty ) {
22 + let ps_command = $"
23 + $newPath = [Environment]::GetEnvironmentVariable\(\"Path\", \"User\"\) + \";($bin)\"
24 + [Environment]::SetEnvironmentVariable\(\"Path\", $newPath, \"User\"\)"
25 + run-external powershell '-EncodedCommand' ($ps_command | endode base64)
26 + $env | upsert Path $bin
27 + }
28 + }
29 + }

nushell/modules/package/provider.nu

Added
new file mode 100644
1 +# Provisions a systems by installing/uninstalling packages in provisions.
2 +# Provisions are a collection of nuon files in PACKAGE_HOME/Provsions/*.
3 +# All files there are expected to list manifests.
4 +# They are collected and installed.
5 +# A precedence order for package sources will be enforced:
6 +# native packages > packages > language packages.
7 +# This precedence will be enforced, even if, packages need to be reinstalled.
8 +# It depends on the availablity of packages in their given source.
10 +# TODO: should i add flatpak to native packages?
12 +use ./folders.nu
13 +use ./path.nu
15 +export def provision [] {
16 + # scan provisions
17 + # check if installed packages are synced with current manifests
18 + # check if current installed packages are installed via correct source
19 + # install all missing. reinstall all with higher precedence
20 + # uninstall non-provisioned ?
21 + # setup path

nushell/modules/package/slinker.nu

Deleted
deleted file mode 100644

No text changes.

tools/packages.nu

Modified
42 42 # Gets value of a column, or falls back to given alternative value.
43 -#
44 43 def get-or-else [
45 44 column: string # name of the column to get its value from
46 45 value: any # alternative value to return, in case `get` gets nothing