Skip to content

Commit 214a7d8

Browse files
committed
nix: add selfoss Nix package
Build client part using napalm and fetch dependencies for the server part using C4.
1 parent 345930b commit 214a7d8

File tree

3 files changed

+186
-2
lines changed

3 files changed

+186
-2
lines changed

flake.lock

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
description = "selfoss feed reader and aggregator";
33

44
inputs = {
5+
# Tool for downloading Composer dependencies using Nix.
6+
c4.url = "github:fossar/composition-c4";
7+
58
# Shim to make flake.nix work with stable Nix.
69
flake-compat = {
710
url = "github:edolstra/flake-compat";
@@ -11,13 +14,18 @@
1114
# Repository with software packages.
1215
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
1316

17+
napalm = {
18+
url = "github:nmattia/napalm";
19+
inputs.nixpkgs.follows = "nixpkgs";
20+
};
21+
1422
utils.url = "github:numtide/flake-utils";
1523

1624
# Package expression for old PHP versions.
1725
phps.url = "github:fossar/nix-phps";
1826
};
1927

20-
outputs = { self, flake-compat, nixpkgs, phps, utils }:
28+
outputs = { self, c4, flake-compat, napalm, nixpkgs, phps, utils }:
2129
let
2230
# Configure the development shell here (e.g. for CI).
2331

@@ -28,7 +36,16 @@
2836
utils.lib.eachDefaultSystem (system:
2937
let
3038
# Get Nixpkgs packages for current platform.
31-
pkgs = nixpkgs.legacyPackages.${system};
39+
pkgs = import nixpkgs {
40+
inherit system;
41+
overlays = [
42+
# Include c4 tool.
43+
c4.overlay
44+
45+
# Include napalm tool.
46+
napalm.overlay
47+
];
48+
};
3249

3350
# Create a PHP package from the selected PHP package, with some extra extensions enabled.
3451
php = phps.packages.${system}.${matrix.phpPackage}.withExtensions ({ enabled, all }: with all; enabled ++ [
@@ -69,6 +86,36 @@
6986
# node-gyp wants some locales, let’s make them available through an environment variable.
7087
LOCALE_ARCHIVE = "${pkgs.glibcLocales}/lib/locale/locale-archive";
7188
};
89+
90+
packages = {
91+
selfoss =
92+
let
93+
filteredSrc = builtins.path {
94+
path = ./.;
95+
filter =
96+
path:
97+
type:
98+
!builtins.elem (builtins.baseNameOf path) [
99+
# These should not be part of the source code for packages built by Nix.
100+
# Otherwise, iterating on Nix files will trigger a rebuild all the time since the source will have changed.
101+
"flake.nix"
102+
"flake.lock"
103+
"utils"
104+
# CI changes should not affect it either.
105+
".github"
106+
];
107+
# Unfortunately, it still triggers a rebuild since any change will cause the flake to be re-cloned.
108+
# https://github.com/NixOS/nix/issues/3732
109+
};
110+
111+
# Due to Nix bug, we cannot actually use the output directly and need to copy it to a new derivation.
112+
# https://github.com/NixOS/nix/issues/3234
113+
src = pkgs.runCommand "selfoss-src" {} "cp -r '${filteredSrc}' $out";
114+
in
115+
pkgs.callPackage ./utils/selfoss.nix rec {
116+
inherit src;
117+
};
118+
};
72119
}
73120
);
74121
}

utils/selfoss.nix

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
stdenv,
3+
napalm,
4+
runCommand,
5+
jq,
6+
lib,
7+
src,
8+
c4,
9+
php,
10+
}:
11+
12+
let
13+
version = (lib.importJSON (src + "/package.json")).ver;
14+
15+
client-src =
16+
runCommand "client-src" {
17+
nativeBuildInputs = [
18+
jq
19+
];
20+
} ''
21+
cp -r "${src}/assets" "$out"
22+
chmod +w "$out"
23+
24+
# napalm requires version for all packages.
25+
# And npm needs it to follow semver.
26+
jq '. += { "name": "selfoss-client", "version": "0.0.0+${version}" }' "$out/package-lock.json" > "$out/package-lock.json.tmp"
27+
mv "$out/package-lock.json.tmp" "$out/package-lock.json"
28+
29+
jq '. += { "name": "selfoss-client", "version": "0.0.0+${version}" }' "$out/package.json" > "$out/package.json.tmp"
30+
mv "$out/package.json.tmp" "$out/package.json"
31+
'';
32+
33+
stopNpmCallingHome = ''
34+
# Do not try to find npm in napalm-registry –
35+
# it is not there and checking will slow down the build.
36+
npm config set update-notifier false
37+
38+
# Same for security auditing, it does not make sense in the sandbox.
39+
npm config set audit false
40+
'';
41+
42+
client-assets = napalm.buildPackage "${client-src}" {
43+
npmCommands = [
44+
# Just download and unpack all the npm packages,
45+
# we need napalm to patch shebangs before we can run install scripts.
46+
"npm install --loglevel verbose --ignore-scripts"
47+
# Let’s install again, this time running scripts.
48+
"npm install --loglevel verbose"
49+
50+
# napalm only patches shebangs for scripts in bin directories
51+
"patchShebangs node_modules/parcel/lib/bin.js"
52+
53+
# Build the front-end.
54+
"npm run build"
55+
];
56+
57+
postConfigure = stopNpmCallingHome;
58+
59+
installPhase = ''
60+
runHook preInstall
61+
62+
mv ../public $out
63+
64+
runHook postInstall
65+
'';
66+
};
67+
in
68+
stdenv.mkDerivation {
69+
pname = "selfoss";
70+
inherit version;
71+
72+
inherit src;
73+
74+
composerDeps = c4.fetchComposerDeps {
75+
inherit src;
76+
};
77+
78+
nativeBuildInputs = [
79+
c4.composerSetupHook
80+
php.packages.composer
81+
];
82+
83+
buildPhase = ''
84+
runHook preBuild
85+
86+
cp -r ${client-assets} public
87+
composer install --no-dev --optimize-autoloader
88+
89+
runHook postBuild
90+
'';
91+
92+
installPhase = ''
93+
runHook preInstall
94+
95+
mkdir -p $out
96+
cp -r . $out
97+
98+
runHook postInstall
99+
'';
100+
}

0 commit comments

Comments
 (0)