Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(default-flatpaks): version 2 (nushell) #336

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions modules/default-flatpaks/v2/default-flatpaks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env nu
xynydev marked this conversation as resolved.
Show resolved Hide resolved

const flathubURL = "https://dl.flathub.org/repo/flathub.flatpakrepo"

const defaultConfiguration = {
xynydev marked this conversation as resolved.
Show resolved Hide resolved
notify: true
scope: user
repo: {
xynydev marked this conversation as resolved.
Show resolved Hide resolved
url: $flathubURL
xynydev marked this conversation as resolved.
Show resolved Hide resolved
xynydev marked this conversation as resolved.
Show resolved Hide resolved
name: "flathub"
title: "Flathub"
}
xynydev marked this conversation as resolved.
Show resolved Hide resolved
install: []
}

const usrSharePath = "/usr/share/bluebuild/default-flatpaks"
const libExecPath = "/usr/libexec/bluebuild/default-flatpaks"
const configPath = $"($usrSharePath)/configuration.yaml"

def main [configStr: string] {
let config = $configStr | from yaml

let configurations = $config.configurations | each {|configuration|
mut merged = $defaultConfiguration | merge $configuration
$merged.repo = $defaultConfiguration.repo | merge $merged.repo # make sure all repo properties exist

print $"Validating configuration of (ansi default_italic)($merged.install | length)(ansi reset) Flatpaks from (ansi default_italic)($merged.repo.title)(ansi reset)"

if (not ($merged.scope == "system" or $merged.scope == "user")) {
print $"(ansi red_bold)Scope must be either(ansi reset) (ansi blue_italic)system(ansi reset) (ansi red_bold)or(ansi reset) (ansi blue_italic)user(ansi reset)"
print $"(ansi blue)Your input:(ansi reset) ($merged.scope)"
exit 1
}
if (not ($merged.notify == true or $merged.notify == false)) {
print $"(ansi red_bold)Notify must be either(ansi reset) (ansi blue_italic)true(ansi reset) (ansi red_bold)or(ansi reset) (ansi blue_italic)false(ansi reset)"
print $"(ansi blue)Your input:(ansi reset) ($merged.notify)"
exit 1
}
if ($merged.repo.url == $flathubURL) {
checkFlathub $merged.install
}

print $"Validation successful!"

$merged
}


if (not ($configPath | path exists)) {
mkdir ($configPath | path dirname)
'[]'| save $configPath
}

open $configPath
| append $configurations
| to yaml | save -f $configPath

print $"(ansi green_bold)Successfully generated following configurations:(ansi reset)"
print ($configurations | to yaml)

print "Setting up Flatpak setup services..."

cp $"($env.MODULE_DIRECTORY)/default-flatpaks/post-boot/system-flatpak-setup.service" /usr/lib/systemd/system/system-flatpak-setup.service
cp $"($env.MODULE_DIRECTORY)/default-flatpaks/post-boot/system-flatpak-setup.timer" /usr/lib/systemd/system/system-flatpak-setup.timer
cp $"($env.MODULE_DIRECTORY)/default-flatpaks/post-boot/user-flatpak-setup.service" /usr/lib/systemd/user/user-flatpak-setup.service
cp $"($env.MODULE_DIRECTORY)/default-flatpaks/post-boot/user-flatpak-setup.timer" /usr/lib/systemd/system/user-flatpak-setup.timer
systemctl enable --force system-flatpak-setup.timer
systemctl enable --force --global user-flatpak-setup.timer

cp $"($env.MODULE_DIRECTORY)/default-flatpaks/post-boot/system-flatpak-setup" $"($libExecPath)/system-flatpak-setup"
cp $"($env.MODULE_DIRECTORY)/default-flatpaks/post-boot/user-flatpak-setup" $"($libExecPath)/user-flatpak-setup"
chmod +x $"($libExecPath)/system-flatpak-setup"
chmod +x $"($libExecPath)/user-flatpak-setup"
}

def checkFlathub [packages: list<string>] {
print "Checking if configured packages exist on Flathub..."
$packages | each { |package|
try {
let _ = http get $"https://flathub.org/apps/($package)"
} catch {
print $"(ansi red_bold)Package(ansi reset) (ansi default_italic)($package)(ansi reset) (ansi red_bold)does not exist on Flathub, which is the specified repository for it to be installed from.(ansi reset)"
exit 1
}
}
}
52 changes: 52 additions & 0 deletions modules/default-flatpaks/v2/post-boot/system-flatpak-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env nu

const usrSharePath = "/usr/share/bluebuild/default-flatpaks"
const configPath = $"($usrSharePath)/configuration.yaml"

def main [] {
let configFile = open $configPath

let systemRemotes = (flatpak remotes --system --columns name | split row "\n")
if ($systemRemotes | any {|remote| $remote == "fedora" or $remote == "fedora-testing"}) {
/usr/bin/gnome-software --quit
/usr/lib/fedora-third-party/fedora-third-party-opt-out
/usr/bin/fedora-third-party disable

if ($systemRemotes | any {|remote| $remote == "fedora"}) {
flatpak remote-delete --system fedora --force
}
if ($systemRemotes | any {|remote| $remote == "fedora-testing"}) {
flatpak remote-delete --system fedora-testing --force
}

# TODO remove Fedora Flatpaks
}

$configFile.configurations | where scope == system | each { |config|
flatpak remote-add --system --if-not-exists $config.repo.name $config.repo.url --title $config.repo.title

if ($config.notify) {
(notify-send-as-user
"--app-name" "Automatic Flatpak Installation Service"
$"Starting automated installation of ($config.install | length) system Flatpak\(s) from ($config.repo.title)..."
)
}

flatpak install --system $config.repo.name ...$config.install

if ($config.notify) {
(notify-send-as-user
"--app-name" "Automatic Flatpak Installation Service"
$"Finished automated installation of ($config.install | length) system Flatpak\(s) from ($config.repo.title)!"
($config.install | str join ', ')
)
}
}
}

def notify-send-as-user [...args] {
let user_name = (loginctl list-sessions --json=short | from json | get user | get 0)
let uid = (loginctl list-sessions --json=short | from json | get uid | get 0)
let xdg_runtime_path = $"/run/user/($uid)"
sudo -u $user_name DBUS_SESSION_BUS_ADDRESS=unix:path=($xdg_runtime_path)/bus notify-send ...$args
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
Description=Manage system flatpaks
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/libexec/bluebuild/default-flatpaks/system-flatpak-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
Description=Timer for system-flatpak-setup

[Timer]
OnBootSec=30

[Install]
WantedBy=timers.target
38 changes: 38 additions & 0 deletions modules/default-flatpaks/v2/post-boot/user-flatpak-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env nu

const usrSharePath = "/usr/share/bluebuild/default-flatpaks"
const configPath = $"($usrSharePath)/configuration.yaml"

def main [] {
let configFile = open $configPath

let userRemotes = (flatpak remotes --user --columns name | split row "\n")
if ($userRemotes | any {|remote| $remote == "fedora"}) {
flatpak remote-delete --user fedora --force
}
if ($userRemotes | any {|remote| $remote == "fedora-testing"}) {
flatpak remote-delete --user fedora-testing --force
}


$configFile.configurations | where scope == user | each { |config|
flatpak remote-add --user --if-not-exists $config.repo.name $config.repo.url --title $config.repo.title

if ($config.notify) {
(notify-send
--app-name "Automatic Flatpak Installation Service"
$"Starting automated installation of ($config.install | length) user Flatpak\(s) from ($config.repo.title)..."
)
}

flatpak install --user $config.repo.name ...$config.install

if ($config.notify) {
(notify-send
--app-name "Automatic Flatpak Installation Service"
$"Finished automated installation of ($config.install | length) user Flatpak\(s) from ($config.repo.title)!"
($config.install | str join ', ')
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
Description=Configure Flatpaks for current user
Wants=network-online.target
After=system-flatpak-setup.service

[Service]
Type=oneshot
ExecStart=/usr/libexec/bluebuild/default-flatpaks/user-flatpak-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
Description=Timer for user-flatpak-setup

[Timer]
OnBootSec=30

[Install]
WantedBy=timers.target