-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an option to declare imports of a GPG key from file. Refactor remotes helper functions in modules/remotes.nix.
- Loading branch information
Showing
7 changed files
with
109 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
# Convert entry value into array | ||
def values($value): if ($value | type) == "string" then [$value] else ($value // []) end; | ||
def values ($value): if ($value | type) == "string" then [$value] else ($value // []) end; | ||
|
||
# State aliases | ||
($old_state.overrides[$app_id] // {}) as $old | ||
($old_state.overrides[$app_id] // {}) as $old | ||
| ($new_state.overrides[$app_id] // {}) as $new | ||
|
||
# Map sections that exist in either active or new state (ignore old) | ||
| $active + $new | keys | map ( | ||
. as $section | {"section_key": $section, "section_value": ( | ||
|
||
# Map entries that exist in either active or new state (ignore old) | ||
($active[$section] // {}) + ($new[$section] // {}) | keys | map ( | ||
. as $entry | { "entry_key": $entry, "entry_value": ( | ||
|
||
# Entry value aliases | ||
$active[$section][$entry] as $active_value | ||
| $new[$section][$entry] as $new_value | ||
| $old[$section][$entry] as $old_value | ||
|
||
# Use new value if it is a string | ||
| if ($new_value | type) == "string" then $new_value | ||
else | ||
# Otherwise remove old values from the active ones, and add the new ones | ||
values($active_value) - values($old_value) + values($new_value) | ||
|
||
# Remove empty arrays and duplicate values | ||
| select(. != []) | unique | ||
# Convert array into Flatpak string array format | ||
| join(";") | ||
end | ||
)} | ||
) | ||
|
||
# Remove empty arrays | ||
| select(. != []) | ||
)} | ||
)[] | ||
. as $section | {"section_key": $section, "section_value": ( | ||
|
||
# Map entries that exist in either active or new state (ignore old) | ||
($active[$section] // {}) + ($new[$section] // {}) | keys | map ( | ||
. as $entry | { "entry_key": $entry, "entry_value": ( | ||
|
||
# Entry value aliases | ||
$active[$section][$entry] as $active_value | ||
| $new[$section][$entry] as $new_value | ||
| $old[$section][$entry] as $old_value | ||
|
||
# Use new value if it is a string | ||
| if ($new_value | type) == "string" then $new_value | ||
else | ||
# Otherwise remove old values from the active ones, and add the new ones | ||
values($active_value) - values($old_value) + values($new_value) | ||
|
||
# Remove empty arrays and duplicate values | ||
| select(. != []) | unique | ||
|
||
# Convert array into Flatpak string array format | ||
| join(";") | ||
end | ||
)} | ||
) | ||
|
||
# Remove empty arrays | ||
| select(. != []) | ||
)} | ||
)[] | ||
|
||
# Generate the final overrides file | ||
| "[\(.section_key)]", (.section_value[] | "\(.entry_key)=\(.entry_value)"), "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ pkgs }: | ||
|
||
let | ||
flatpakAddRemotesCmd = installation: { name, location, gpg-import ? null, args ? null, ... }: | ||
let | ||
gpg-import-flag = ''${if gpg-import == null then "" else "--gpg-import=${gpg-import}" }''; | ||
args-flag = ''${if args == null then "" else args}''; | ||
in | ||
'' | ||
${pkgs.flatpak}/bin/flatpak remote-add --${installation} --if-not-exists ${args-flag} ${gpg-import-flag} ${name} ${location} | ||
''; | ||
|
||
flatpakAddRemote = installation: remotes: map (flatpakAddRemotesCmd installation) remotes; | ||
|
||
flatpakDeleteRemotesCmd = installation: uninstallUnmanaged: {}: '' | ||
# Delete all remotes that are present in the old state but not the new one | ||
# $OLD_STATE and $NEW_STATE are globals, declared in the output of pkgs.writeShellScript. | ||
# If uninstallUnmanagedState is true, then the remotes will be deleted forcefully. | ||
${pkgs.jq}/bin/jq -r -n \ | ||
--argjson old "$OLD_STATE" \ | ||
--argjson new "$NEW_STATE" \ | ||
'(($old.remotes // []) - ($new.remotes // []))[]' \ | ||
| while read -r REMOTE_NAME; do | ||
${pkgs.flatpak}/bin/flatpak remote-delete ${if uninstallUnmanaged then " --force " else " " } --${installation} $REMOTE_NAME | ||
done | ||
''; | ||
|
||
mkFlatpakAddRemotesCmd = installation: remotes: builtins.foldl' (x: y: x + y) '''' (flatpakAddRemote installation remotes); | ||
in | ||
{ | ||
inherit mkFlatpakAddRemotesCmd flatpakDeleteRemotesCmd; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
cd tests/ | ||
|
||
test_count=0 | ||
for test_file in *-test.nix; do | ||
echo "Collecting results for... ${test_file}" | ||
result=$(nix eval --show-trace --impure --expr "import ./${test_file} {}") | ||
|
||
if [ "$result" != "[ ]" ]; then | ||
echo "Test failed: Expected [], but got $result in $test_file" | ||
exit 1 | ||
fi | ||
((test_count++)) | ||
done | ||
|
||
echo "All tests in ${test_count} suites passed." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ pkgs ? import <nixpkgs> { } }: | ||
|
||
let | ||
inherit (pkgs) lib; | ||
inherit (lib) runTests; | ||
installation = "system"; | ||
installer = import ../modules/remotes.nix { inherit pkgs; }; | ||
in | ||
runTests { | ||
testMkFlatpakAddRemotesCmd = { | ||
expr = installer.mkFlatpakAddRemotesCmd installation [{ name = "flathub"; location = "http://flathub"; }]; | ||
expected = "${pkgs.flatpak}/bin/flatpak remote-add --system --if-not-exists flathub http://flathub\n"; | ||
}; | ||
|
||
testMkFlatpakAddRemotesCmdCmdWithTrustedKeys = { | ||
expr = installer.mkFlatpakAddRemotesCmd installation [{ name = "flathub"; location = "http://flathub"; gpg-import = "trustedkeys.gpg"; }]; | ||
expected = "${pkgs.flatpak}/bin/flatpak remote-add --system --if-not-exists --gpg-import=trustedkeys.gpg flathub http://flathub\n"; | ||
}; | ||
} |