From 41c7beef647d9d252940b573b03270dc89c1d1a8 Mon Sep 17 00:00:00 2001 From: ChUrl Date: Mon, 8 Aug 2022 01:55:10 +0200 Subject: [PATCH] add flatpak auto installation/removal --- home/home.nix | 6 ++- home/modules/flatpak.nix | 93 ++++++++++++++++++++++++++++++++-------- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/home/home.nix b/home/home.nix index f97da773..612fa5b7 100644 --- a/home/home.nix +++ b/home/home.nix @@ -47,10 +47,12 @@ rec { fontFix = true; iconFix = true; - autoInstall = true; autoUpdate = true; + autoPrune = true; - packages = [ "discord" ]; + discord = false; + spotify = true; + flatseal = true; }; # TODO: Email diff --git a/home/modules/flatpak.nix b/home/modules/flatpak.nix index 7d41852b..5811219e 100644 --- a/home/modules/flatpak.nix +++ b/home/modules/flatpak.nix @@ -26,23 +26,43 @@ in { description = "Link icons to ~/.local/share/icons so flatpak apps can find them"; }; - packages = mkOption { - type = types.listOf types.str; - default = [ ]; - description = "List of enabled flatpaks"; - }; - - autoInstall = mkOption { - type = types.bool; - default = false; - description = "Install enabled flatpaks on nixos-rebuild"; - }; - autoUpdate = mkOption { type = types.bool; default = false; description = "Update flatpak apps on nixos-rebuild"; }; + + autoPrune = mkOption { + type = types.bool; + default = false; + description = "Remove unused packages on nixos-rebuild"; + }; + + discord = mkOption { + type = types.bool; + default = false; + description = "Enable Discord"; + }; + + spotify = mkOption { + type = types.bool; + default = false; + description = "Enable Spotify"; + }; + + flatseal = mkOption { + type = types.bool; + default = false; + description = "Enable Flatseal"; + }; + + # NOTE: Disabled as these won't be removed automatically when disabled + # Will see if it works well enough without this flexibility + # extraPackages = mkOption { + # type = types.listOf types.str; + # default = [ ]; + # description = "List of additionally enabled flatpaks"; + # }; }; config = mkIf cfg.enable { @@ -73,15 +93,52 @@ in { ''; }) - (mkIf cfg.autoInstall { - installFlatpak = lib.hm.dag.entryAfter [ "writeBoundary" ] '' - flatpak install -y ${builtins.concatStringsSep " " cfg.packages} - ''; - }) + { + installFlatpak = + let + # For some reason (mkIf ...) evaluates to a set { _type = "if"; condition = ... } so nothing can + # be merged. I don't understand why the set isn't evaluated, it worked previously + to_install = (builtins.concatLists [ + # (mkIf cfg.discord [ "com.discordapp.Discord" ]) + # (mkIf cfg.spotify [ "com.spotify.Client" ]) + # (mkIf cfg.flatseal [ "com.github.tchx84.Flatseal" ]) + (if cfg.discord then [ "com.discordapp.Discord" ] else [ ]) + (if cfg.spotify then [ "com.spotify.Client" ] else [ ]) + (if cfg.flatseal then [ "com.github.tchx84.Flatseal" ] else [ ]) + # cfg.extraPackages + ]); + + to_install_str = (builtins.concatStringsSep " " to_install); + in + lib.hm.dag.entryAfter [ "writeBoundary" ] '' + sudo flatpak install -y ${to_install_str} + ''; + + # NOTE: This only removes named flatpaks, nothing from extraPackages + removeFlatpak = + let + to_remove = (builtins.concatLists [ + (if ! cfg.discord then [ "com.discordapp.Discord" ] else [ ]) + (if ! cfg.spotify then [ "com.spotify.Client" ] else [ ]) + (if ! cfg.flatseal then [ "com.github.tchx84.Flatseal" ] else [ ]) + ]); + + to_remove_str = (builtins.concatStringsSep " " to_remove); + in + lib.hm.dag.entryAfter [ "writeBoundary" ] '' + sudo flatpak uninstall -y ${to_remove_str} + ''; + } (mkIf cfg.autoUpdate { updateFlatpak = lib.hm.dag.entryAfter [ "writeBoundary" ] '' - flatpak update -y + sudo flatpak update -y + ''; + }) + + (mkIf cfg.autoPrune { + pruneFlatpak = lib.hm.dag.entryAfter [ "writeBoundary" "removeFlatpak" ] '' + sudo flatpak uninstall --unused -y ''; }) ]);