@@ -0,0 +1,131 @@
1+{
2+ description = "Flake utils demo";
3+
4+ inputs = {
5+ flake-utils.url = "github:numtide/flake-utils";
6+ nix-bundle = {
7+ url =
8+ "github:ilyakooo0/nix-bundle?rev=db4b8ca5af5ccdb3a4ff8be4a2390fecc7c58134";
9+ flake = false;
10+ };
11+ };
12+
13+ outputs = { self, nixpkgs, flake-utils, nix-bundle }:
14+ flake-utils.lib.eachDefaultSystem (system:
15+ let pkgs = import nixpkgs { inherit system; };
16+ in {
17+ packages = rec {
18+ buildAll = { name, supportedPlatforms ? {
19+ arm64 = true;
20+ amd64 = false;
21+ armhf = false;
22+ } }:
23+ f:
24+ let
25+ f_ = { arch, staticPkgs, targetSystem }:
26+ let args = f staticPkgs;
27+ in if args == null || !(supportedPlatforms ? ${arch})
28+ || supportedPlatforms.${arch} == null
29+ || !supportedPlatforms.${arch} then
30+ null
31+ else {
32+ name = "${arch}.click";
33+ path = build (args // {
34+ manifest = args.manifest // { architecture = arch; };
35+ inherit targetSystem;
36+ });
37+ };
38+ in pkgs.linkFarm name (builtins.filter (x: x != null) (builtins.map
39+ (args:
40+ let res = builtins.tryEval (f_ args);
41+ in if res.success then res.value else null) [{
42+ arch = "arm64";
43+ staticPkgs = (import nixpkgs { system = "aarch64-linux"; });
44+ targetSystem = "aarch64-linux";
45+ }
46+ {
47+ arch = "armhf";
48+ staticPkgs = (import nixpkgs { system = "armv7l-linux"; });
49+ targetSystem = "armv7l-linux";
50+ }
51+ {
52+ arch = "amd64";
53+ staticPkgs = (import nixpkgs { system = "x86_64-linux"; });
54+ targetSystem = "x86_64-linux";
55+ }
56+ ]));
57+ build = { targetSystem, exec, contents ? [ ], manifest ? null
58+ , apparmor, icon ? ./icon.svg }:
59+ let
60+ contentsList = if builtins.typeOf contents == "list" then
61+ contents
62+ else
63+ [ contents ];
64+ getObj = src: arg:
65+ if arg == null then
66+ # should be read from src
67+ builtins.fromJSON (builtins.readFile src)
68+ else
69+ # specified as an argument
70+ if builtins.typeOf arg == "path" then
71+ builtins.fromJSON (builtins.readFile manifest)
72+ else if builtins.typeOf arg == "set" then
73+ arg
74+ else
75+ builtins.throw "Could not read ${src}";
76+ desktopName = "${manifestObjectRaw.name}.desktop";
77+ apparomrName = "${manifestObjectRaw.name}.apparmor";
78+ desktopFile = pkgs.writeTextFile {
79+ name = desktopName;
80+ text = ''
81+ [Desktop Entry]
82+ Name=${manifestObject.title}
83+ Exec=nix-user-chroot -n ./nix -- ${exec}
84+ Icon=${builtins.baseNameOf icon}
85+ Terminal=false
86+ Type=Application
87+ X-Ubuntu-Touch=true
88+ '';
89+ };
90+ manifestObjectRaw = getObj "manifest.json" manifest;
91+ manifestObject = manifestObjectRaw // {
92+ hooks = {
93+ "${manifestObjectRaw.name}" = {
94+ apparmor = apparomrName;
95+ desktop = desktopName;
96+ };
97+ };
98+ };
99+ manifestFile = pkgs.writeTextFile {
100+ name = "manifest.json";
101+ text = builtins.toJSON manifestObject;
102+ };
103+ allDeps = pkgs.concatTextFile {
104+ name = "${manifest.name}-deps";
105+ files = builtins.map pkgs.writeReferencesToFile
106+ (contentsList ++ [ exec ]);
107+ };
108+ targetPkgs = import nixpkgs { system = targetSystem; };
109+ nix-user-chroot = (import nix-bundle {
110+ nixpkgs = targetPkgs.pkgsStatic;
111+ }).nix-user-chroot.override { postFixup = ""; };
112+ in pkgs.runCommand "${manifest.name}-${manifest.version}.click"
113+ { } ''
114+ cat ${allDeps} | uniq >deps
115+ mkdir build
116+ while read p; do
117+ echo "copying $p"
118+ cp -r --parents $p build
119+ done <deps
120+ cp ${nix-user-chroot}/bin/nix-user-chroot build/nix-user-chroot
121+ cp ${apparmor} build/${apparomrName}
122+ cp ${desktopFile} build/${desktopName}
123+ cp ${manifestFile} build/manifest.json
124+ cp ${icon} build/${builtins.baseNameOf icon}
125+ chmod -R 777 build
126+ ${pkgs.ubports-click}/bin/click build build
127+ cp *.click $out
128+ '';
129+ };
130+ });
131+}