nix-flatpak

repository·main·Indexed 21 days ago

https://github.com/gmodena/nix-flatpak

A declarative Flatpak manager for NixOS and Home Manager that allows users to manage Flatpak applications, remotes, and overrides through Nix configuration files. It supports installation via Nix Flakes or fetchFromGitHub and provides a convergent mode approach to bridge the Nix and Flatpak ecosystems.

Tokens
3.4K
Snippets
12
Records
17
Agent score
25%

What's inside nix-flatpak

  1. Manage Flatpak overrides with settings and files

    main

    You can modify application permissions and environment variables using two methods. Warning: Using these settings will overwrite existing system/user Flatpak override files. Backup your files first.

    Inline Settings

    Use services.flatpak.overrides.settings to declare overrides directly in Nix. This is useful for environment variables and context settings (sockets, filesystems, etc.).

    External Files

    Use services.flatpak.overrides.files to load standard Flatpak override INI files. The filename must match the application ID (e.g., com.visualstudio.code).

    Merging and Precedence

    If both are used for the same application, they are merged. settings take precedence over files. Both are merged with any existing overrides on disk.

    Write Modes

    • "merge" (default): Merges Nix-managed settings with existing on-disk files. User edits to non-Nix keys are preserved.
    • "replace": The file is computed as a pure store derivation and replaces the on-disk file entirely. nix-flatpak fully owns the file; manual edits will be overwritten.
    # Example: Combining settings and files
    {
      services.flatpak.overrides = {
        files = [ "/path/to/overrides.d/com.visualstudio.code" ];
        settings."com.visualstudio.code".Context.sockets = ["gpg-agent"];
      };
    }
  2. How nix-flatpak works (Convergent Mode)

    main

    Unlike purely declarative systems, nix-flatpak follows a convergent mode approach. This means the target system state description is not exhaustive; there is room for divergence across builds and rollbacks.

    This approach allows for flexibility, such as tracking the latest versions of desktop applications or allowing them to auto-update, which is a tradeoff against absolute system reproducibility.

    Note on performance: Flatpak applications are installed via a systemd oneshot service triggered at system activation. If you have a large number of applications, this may significantly increase your system's activation time.

  3. Use the testing base to experiment with nix-flatpak

    main

    The testing-base repository provides a pre-configured NixOS environment (GDM + Gnome) designed for experimenting with nix-flatpak. It allows you to test both the NixOS module and the Home Manager module using QEMU virtual machines.

    Configuration Structure

    • flake.nix: Provides two distinct outputs:
      • A NixOS module installation.
      • A Home Manager module installation.
    • configuration.nix: Contains system-level configurations (QEMU specs, users, SSH, etc.).
    • flatpak.nix: Contains a sample nix-flatpak configuration.

    Build and Run Instructions

    1. Build the VM

    To test the NixOS module setup:

    nix build .#nixosConfigurations.test-system-module.config.system.build.vm

    To test the Home Manager module setup:

    nix build .#nixosConfigurations.test-hm-module.config.system.build.vm

    2. Start the VM

    Before running the VM, export the network options to enable SSH port forwarding, then execute the generated runner:

    export QEMU_NET_OPTS="hostfwd=tcp::2221-:22"
    result/bin/run-nixos-vm

    3. Access the VM

    Via GDM: Use the credentials provided below.

    Via SSH:

    ssh -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no antani@localhost -p 2221

    Credentials:

    • Username: antani
    • Password: changeme

    4. Verify Flatpaks

    Once logged in, you can monitor the state of installed applications using:

    flatpak list
    nix build .#nixosConfigurations.test-system-module.config.system.build.vm
  4. Install nix-flatpak manually via fetchFromGitHub

    main

    If you are not using Flakes, you can manually fetch nix-flatpak using pkgs.fetchFromGitHub. You will need to provide the correct rev (version tag) and hash.

    You can generate the required hash using the nix-prefetch-github command:

    nix-prefetch-github gmodena nix-flatpak --rev <rev>
    let
      nix-flatpak = pkgs.fetchFromGitHub {
        owner = "gmodena";
        repo = "nix-flatpak";
        rev = "v0.7.0";
        hash = "sha256-7ZCulYUD9RmJIDULTRkGLSW1faMpDlPKcbWJLYHoXcs=";
      };
    in
      imports = [ "${nix-flatpak}/modules/nixos.nix" ];
  5. Configure override write mode and purity

    main

    When using services.flatpak.overrides.files, the method of referencing files affects whether your configuration is 'pure':

    • Impure (String paths): Using a string like "/home/user/overrides/app.ini" requires passing --impure to nixos-rebuild because the file is read from the filesystem at evaluation time.
    • Pure (Nix path literals): Using a path literal like ./overrides/app.ini copies the file into the Nix store during evaluation, making the configuration reproducible and pure.

    Note: writeMode = "replace" with only settings (no files) is always pure.

    # Required if using string paths in overrides.files
    sudo nixos-rebuild switch --impure
  6. Install nix-flatpak using Flakes

    main

    The recommended way to install nix-flatpak is via Nix Flakes. You can pin to a specific stable release using the ref parameter to ensure reproducibility, or use the latest tag. Using the default URL points to the main branch, which is unstable.

    To install as a NixOS module, add the input to your flake.nix and include nix-flatpak.nixosModules.nix-flatpak in your system modules list.

    inputs = {
      # Pin to a specific release
      nix-flatpak.url = "github:gmodena/nix-flatpak/?ref=v0.7.0";
      
      # OR use the latest stable tag
      # nix-flatpak.url = "github:gmodena/nix-flatpak/?ref=latest";
    
      # OR use the unstable main branch
      # nix-flatpak.url = "github:gmodena/nix-flatpak";
    };
    
    outputs = { nixpkgs, nix-flatpak, ... }: {
      nixosConfigurations.my-host = nixpkgs.lib.nixosSystem {
        modules = [
          nix-flatpak.nixosModules.nix-flatpak
          ./configuration.nix
        ];
      };
    };
  7. Install Flatpak bundles

    main

    Bundle files (.flatpak) can be installed by pointing the bundle attribute to a URI (using pkgs.fetchurl) or a local file path. A sha256 hash is required.

    # From a URI
    services.flatpak.packages = [
      rec {
        appId = "<appId>";
        sha256 = "<hash>";
        bundle = "${pkgs.fetchurl {
          url = "<bundle-uri>";
          inherit sha256;
        }}";
      }
    ];
    
    # From a local file
    services.flatpak.packages = [
      {
        bundle = "file:///path/to/local/app.flatpak";
        appId = "<appId>";
        sha256 = "<hash>";
      }
    ];
  8. Manage unmanaged Flatpak packages and remotes

    main

    By default, nix-flatpak only manages packages and remotes explicitly declared in your configuration. Packages installed via app stores or CLI will be ignored.

    To make nix-flatpak manage the entire lifecycle (including uninstallation of undeclared items), set services.flatpak.uninstallUnmanaged = true.

    Scope:

    • If installed as a HomeManager module, it only affects user installations (~/.local/share/flatpak/).
    • If installed as a NixOS module, it only affects system installations (/var/lib/flatpak/).
  9. Prune unmanaged override files

    main

    By default, when you remove an override from your Nix configuration, the corresponding file on disk is preserved. To ensure a clean state where only declared overrides exist, set services.flatpak.overrides.pruneUnmanagedOverrides = true;.

    Warning: This can result in data loss if you have manual overrides you intended to keep. Use with caution.

  10. Configure Flatpak updates

    main

    You can control how and when Flatpak applications are updated:

    1. On Activation: Set services.flatpak.update.onActivation = true; to update during system/user activation. The default is false to ensure idempotency.
    2. Periodic Updates: Enable scheduled updates via services.flatpak.update.auto. This uses systemd timers. onCalendar accepts standard systemd timer expressions.
    # Update on activation
    services.flatpak.update.onActivation = true;
    
    # Periodic updates
    services.flatpak.update.auto = {
      enable = true;
      onCalendar = "weekly";
    };
  11. Install Flatpak via flatpakref files

    main

    You can install applications using .flatpakref files by setting the flatpakref attribute. A sha256 hash is required. If omitted, the flake evaluation will require the --impure flag.

    When installing via flatpakref, the remote is determined by the SuggestRemoteName key in the file, or by sanitizing the Name key if no suggestion exists.

    services.flatpak.packages = [
      { flatpakref = "<uri>"; sha256="<hash>"; }
    ];