├── flake.nix ├── jump-to-fetch-flatpak.sh ├── fetch-flatpak.sh ├── flake.lock ├── LICENSE ├── README.md └── flatpak.nix /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "declarative flatpak"; 3 | 4 | outputs = { self, nixpkgs }: rec { 5 | 6 | lib.flatpak = with import nixpkgs { system = "x86_64-linux"; }; callPackage ./flatpak.nix { }; 7 | 8 | }; 9 | } 10 | -------------------------------------------------------------------------------- /jump-to-fetch-flatpak.sh: -------------------------------------------------------------------------------- 1 | source $stdenv/setup 2 | 3 | set -x 4 | 5 | export FLATPAK_DIR="$HOME/.local/share/flatpak" 6 | 7 | if [ -n "$runtime" ]; then 8 | bwrap --dev-bind / / --ro-bind $runtime "${FLATPAK_DIR}/runtime" bash $fetcher 9 | else 10 | bash $fetcher 11 | fi 12 | -------------------------------------------------------------------------------- /fetch-flatpak.sh: -------------------------------------------------------------------------------- 1 | set -x 2 | 3 | mkdir $out 4 | 5 | mkdir -p ${FLATPAK_DIR} 6 | 7 | flatpak --user remote-add --no-gpg-verify flathub https://flathub.org/repo/flathub.flatpakrepo 8 | flatpak --user -y install $ref 9 | 10 | flatpak --user -y update --commit=$commit $ref 11 | 12 | if [ -d "${FLATPAK_DIR}/app" ]; then 13 | mv ${FLATPAK_DIR}/app/$name $out/ 14 | else 15 | mv ${FLATPAK_DIR}/runtime/* $out/ 16 | fi 17 | 18 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "nixpkgs": { 4 | "locked": { 5 | "lastModified": 1661353537, 6 | "narHash": "sha256-1E2IGPajOsrkR49mM5h55OtYnU0dGyre6gl60NXKITE=", 7 | "owner": "NixOS", 8 | "repo": "nixpkgs", 9 | "rev": "0e304ff0d9db453a4b230e9386418fd974d5804a", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "id": "nixpkgs", 14 | "type": "indirect" 15 | } 16 | }, 17 | "root": { 18 | "inputs": { 19 | "nixpkgs": "nixpkgs" 20 | } 21 | } 22 | }, 23 | "root": "root", 24 | "version": 7 25 | } 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 yawnt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | declarative-nix-flatpak 2 | ======================= 3 | 4 | install flatpak, the nix way! 5 | 6 | ```nix 7 | { 8 | description = "Plex HTPC"; 9 | 10 | inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; 11 | inputs.flatpak.url = "github:yawnt/declarative-nix-flatpak/main"; 12 | 13 | outputs = { self, nixpkgs, flatpak }: { 14 | 15 | defaultPackage.x86_64-linux = 16 | let 17 | runtime = flatpak.lib.flatpak.fetchRuntimeFromFlatHub { 18 | name = "org.freedesktop.Platform"; 19 | branch = "21.08"; 20 | commit = "c7252386179c4c1ecf5d93bdaec6ca82852dddfdae7112b5c3177f7424c4a928"; 21 | sha256 = "sha256-wuFiB2+x+Mj6FL9GgrknvD9Su6bWr5QVhgcB8/4uBQc="; 22 | }; 23 | app = flatpak.lib.flatpak.fetchAppFromFlatHub { 24 | name = "tv.plex.PlexHTPC"; 25 | commit = "2e5a23587e69324c32c99c50e9c235d17cc7157b9f4f51bed4655401491f35bc"; 26 | sha256 = "sha256-yxnsiou/RldhfdP4D7J7i00uNGm0TjHgrIp0TTwUq3o="; 27 | inherit runtime; 28 | }; 29 | in 30 | flatpak.lib.flatpak.wrapFlatpakLauncher "tv.plex.plexHTPC" app runtime; 31 | }; 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /flatpak.nix: -------------------------------------------------------------------------------- 1 | { pkgs, stdenvNoCC }: 2 | 3 | let 4 | wrapFlatpakLauncher = name: app: runtime: pkgs.writeScriptBin "${name}" '' 5 | FLATPAK_DIR=$HOME/.local/share/flatpak 6 | ${pkgs.bubblewrap}/bin/bwrap \ 7 | --dev-bind / / \ 8 | --tmpfs $FLATPAK_DIR \ 9 | --ro-bind ${app} $FLATPAK_DIR/app \ 10 | --ro-bind ${runtime} $FLATPAK_DIR/runtime \ 11 | ${pkgs.flatpak}/bin/flatpak --user run ${name} 12 | ''; 13 | fetchFromFlatHub = 14 | { name, runtime ? null, ref, commit, sha256 }: 15 | stdenvNoCC.mkDerivation { 16 | nativeBuildInputs = [ pkgs.flatpak pkgs.cacert pkgs.bubblewrap ]; 17 | 18 | runtime = if runtime != null then runtime else ""; 19 | builder = ./jump-to-fetch-flatpak.sh; 20 | fetcher = ./fetch-flatpak.sh; 21 | 22 | outputHashAlgo = "sha256"; 23 | outputHashMode = "recursive"; 24 | outputHash = sha256; 25 | 26 | inherit name ref commit; 27 | }; 28 | fetchRuntimeFromFlatHub = 29 | { name, arch ? "x86_64", branch, commit, sha256 }: 30 | fetchFromFlatHub { ref = "${name}/${arch}/${branch}"; inherit name commit sha256; }; 31 | fetchAppFromFlatHub = 32 | { name, arch ? "x86_64", branch ? "stable", runtime, commit, sha256 }: 33 | fetchFromFlatHub { ref = "${name}/${arch}/${branch}"; inherit name runtime commit sha256; }; 34 | in 35 | { 36 | inherit fetchRuntimeFromFlatHub fetchAppFromFlatHub wrapFlatpakLauncher; 37 | } 38 | --------------------------------------------------------------------------------