chore(nix): nixify

This does almost everything until the final build and fails there
because we are using too modern software with a too old project, I
think.

Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
This commit is contained in:
Raito Bezarius 2024-09-05 16:57:52 +02:00
parent 7a61d9f952
commit a759cc912f
40 changed files with 1551 additions and 36 deletions

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 416 KiB

After

Width:  |  Height:  |  Size: 416 KiB

View file

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 41 KiB

View file

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View file

Before

Width:  |  Height:  |  Size: 9 KiB

After

Width:  |  Height:  |  Size: 9 KiB

View file

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View file

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 89 KiB

View file

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View file

Before

Width:  |  Height:  |  Size: 102 KiB

After

Width:  |  Height:  |  Size: 102 KiB

View file

Before

Width:  |  Height:  |  Size: 403 KiB

After

Width:  |  Height:  |  Size: 403 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

29
android/build.gradle Normal file
View file

@ -0,0 +1,29 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
if(project.hasProperty("nixMavenRepo")) {
repositories {
maven { url = nixMavenRepo }
}
} else {
repositories {
google()
jcenter()
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
}
}
allprojects {
if(project.hasProperty("nixMavenRepo")) {
repositories {
maven { url = nixMavenRepo }
}
} else {
repositories {
google()
jcenter()
}
}
}

View file

View file

@ -1,17 +0,0 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}

39
nix/build.nix Normal file
View file

@ -0,0 +1,39 @@
{ lib
, stdenv
, jdk
, gradle
, mavenRepo
, androidSdk
}:
stdenv.mkDerivation {
pname = "built-with-gradle";
version = "0.0";
src = ../android;
nativeBuildInputs = [ gradle ];
JDK_HOME = "${jdk.home}";
ANDROID_SDK_ROOT = "${androidSdk}/share/android-sdk";
buildPhase = ''
runHook preBuild
gradle build \
--offline --no-daemon --no-build-cache --info --full-stacktrace \
--warning-mode=all --parallel \
-PnixMavenRepo=${mavenRepo} \
-Dorg.gradle.project.android.aapt2FromMavenOverride=$ANDROID_SDK_ROOT/build-tools/30.0.3/aapt2
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r app/build/outputs/* $out
runHook postInstall
'';
dontStrip = true;
}

32
nix/default.nix Normal file
View file

@ -0,0 +1,32 @@
{ sources ? import ../npins, pkgs ? import sources.nixpkgs {}, lib ? pkgs.lib, android ? import sources.android {} }:
let
androidSdk = android.sdk (sdkPkgs: with sdkPkgs; [
cmdline-tools-latest
build-tools-30-0-3
platform-tools
platforms-android-30
emulator
]);
in lib.makeScope pkgs.newScope (self: with self; {
inherit androidSdk;
gradle = pkgs.gradle_7;
updateLocks = callPackage ./update-locks.nix { };
buildMavenRepo = callPackage ./maven-repo.nix { };
mavenRepo = buildMavenRepo {
name = "nix-maven-repo";
repos = [
"https://dl.google.com/dl/android/maven2"
"https://repo1.maven.org/maven2"
"https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev"
"https://plugins.gradle.org/m2"
];
deps = builtins.fromJSON (builtins.readFile ./deps.json);
};
builtWithGradle = callPackage ./build.nix { };
})

1158
nix/deps.json Normal file

File diff suppressed because it is too large Load diff

172
nix/maven-repo.nix Normal file
View file

@ -0,0 +1,172 @@
{ lib
, stdenv
, buildEnv
, fetchurl
, writeTextDir
}:
{ name ? "maven-deps"
, repos ? [ ]
, deps ? [ ]
, extraPaths ? [ ]
}:
with lib;
let
mavenize = sep: replaceStrings ["."] [sep];
fetch =
{ group
, name
, version
, file
, sha256
}:
fetchurl {
name = file;
urls = map (repo: "${repo}/${mavenize "/" group}/${name}/${version}/${file}") repos;
inherit sha256;
meta.platforms = platforms.all;
};
fetchDependency =
{ group
, name
, version
, artifacts
}:
let
fetchArtifact = file: sha256:
fetch { inherit group name version file sha256; };
# Each artifact uses the filename in the Gradle cache, which doesn't
# correspond to the filename in the Maven repo. The mapping of name to URL
# is provided by Gradle module metadata, so we fetch that first. See
# https://github.com/gradle/gradle/blob/master/subprojects/docs/src/docs/design/gradle-module-metadata-latest-specification.md
# for the file format.
isModule = hasSuffix ".module";
moduleArtifacts = filterAttrs (file: _: isModule file) artifacts;
otherArtifacts = filterAttrs (file: _: !isModule file) artifacts;
modules = mapAttrsToList fetchArtifact moduleArtifacts;
replacements = listToAttrs (flatten (map (module:
let
json = builtins.fromJSON (builtins.readFile module);
variants = json.variants or [ ];
files = flatten (map (v: v.files or [ ]) variants);
in
map ({ name, url, ... }: nameValuePair name url) files
) modules));
replaced = mapAttrs' (file: sha256:
nameValuePair (replacements.${file} or file) sha256
) otherArtifacts;
in
if moduleArtifacts == { }
then mapAttrsToList fetchArtifact artifacts
else modules ++ (mapAttrsToList fetchArtifact replaced);
mkDep =
{ group
, name
, version
, artifacts
}@dep:
stdenv.mkDerivation {
pname = "${mavenize "-" group}-${name}";
inherit version;
srcs = fetchDependency dep;
sourceRoot = ".";
phases = "installPhase";
enableParallelBuilding = true;
preferLocalBuild = true;
installPhase = ''
dest=$out/${mavenize "/" group}/${name}/${version}
mkdir -p $dest
for src in $srcs; do
cp $src $dest/$(stripHash $src)
done
'';
};
mkMetadata = deps:
let
modules = groupBy'
(meta: { group, name, version, ... }:
let
isNewer = versionOlder meta.latest version;
isNewerRelease = versionOlder meta.release version;
in {
groupId = group;
artifactId = name;
latest = if isNewer then version else meta.latest;
release = if isNewerRelease then version else meta.release;
versions = meta.versions ++ [ version ];
}
)
{
latest = "";
release = "";
versions = [ ];
}
({ group, name, ... }: "${mavenize "/" group}/${name}/maven-metadata.xml")
deps;
in
attrValues (mapAttrs (path: { groupId, artifactId, latest, release, versions }:
let
versions' = sort versionOlder (unique versions);
in
writeTextDir path ''
<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1">
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<versioning>
${optionalString (latest != "") "<latest>${latest}</latest>"}
${optionalString (release != "") "<release>${release}</release>"}
<versions>
${concatMapStringsSep "\n " (v: "<version>${v}</version>") versions'}
</versions>
</versioning>
</metadata>
''
) modules);
mkGradleRedirectionPoms = deps:
let
depsMissingPoms = filter ({ artifacts, ... }@dep:
any (f: hasSuffix ".module" f) (attrNames artifacts) &&
!(any (f: hasSuffix ".pom" f) (attrNames artifacts))
) deps;
in
map ({ group, name, version, ... }:
writeTextDir "${mavenize "/" group}/${name}/${version}/${name}-${version}.pom" ''
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>4.0.0</modelVersion>
<groupId>${group}</groupId>
<artifactId>${name}</artifactId>
<version>${version}</version>
</project>
''
) depsMissingPoms;
in
buildEnv {
inherit name;
paths = map mkDep deps ++ mkMetadata deps ++ mkGradleRedirectionPoms deps ++ extraPaths;
}

1
nix/result Symbolic link
View file

@ -0,0 +1 @@
/nix/store/ya607qgczf0zbsldarb88qqw3cq2yslp-nix-maven-repo

17
nix/update-locks.nix Normal file
View file

@ -0,0 +1,17 @@
{ lib
, writeShellScriptBin
, gradle
, jq
, yq-go
}:
writeShellScriptBin "update-locks" ''
set -eu -o pipefail
set -x
${gradle}/bin/gradle dependencies --write-locks
${gradle}/bin/gradle --write-verification-metadata sha256 dependencies
${yq-go}/bin/yq -p=xml -o=json gradle/verification-metadata.xml \
| ${jq}/bin/jq '."verification-metadata".components.component' \
| ${jq}/bin/jq '[ .[] | { group: ."+@group", name: ."+@name", version: ."+@version", artifacts: [([.artifact] | flatten | .[] | {(."+@name"): .sha256."+@value"})] | add } ]' > deps.json
rm gradle/verification-metadata.xml
''

80
npins/default.nix Normal file
View file

@ -0,0 +1,80 @@
# Generated by npins. Do not modify; will be overwritten regularly
let
data = builtins.fromJSON (builtins.readFile ./sources.json);
version = data.version;
mkSource =
spec:
assert spec ? type;
let
path =
if spec.type == "Git" then
mkGitSource spec
else if spec.type == "GitRelease" then
mkGitSource spec
else if spec.type == "PyPi" then
mkPyPiSource spec
else if spec.type == "Channel" then
mkChannelSource spec
else
builtins.throw "Unknown source type ${spec.type}";
in
spec // { outPath = path; };
mkGitSource =
{
repository,
revision,
url ? null,
hash,
branch ? null,
...
}:
assert repository ? type;
# At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
# In the latter case, there we will always be an url to the tarball
if url != null then
(builtins.fetchTarball {
inherit url;
sha256 = hash;
})
else
assert repository.type == "Git";
let
urlToName =
url: rev:
let
matched = builtins.match "^.*/([^/]*)(\\.git)?$" repository.url;
short = builtins.substring 0 7 rev;
appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
in
"${if matched == null then "source" else builtins.head matched}${appendShort}";
name = urlToName repository.url revision;
in
builtins.fetchGit {
url = repository.url;
rev = revision;
inherit name;
narHash = hash;
};
mkPyPiSource =
{ url, hash, ... }:
builtins.fetchurl {
inherit url;
sha256 = hash;
};
mkChannelSource =
{ url, hash, ... }:
builtins.fetchTarball {
inherit url;
sha256 = hash;
};
in
if version == 4 then
builtins.mapAttrs (_: mkSource) data.pins
else
throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"

23
npins/sources.json Normal file
View file

@ -0,0 +1,23 @@
{
"pins": {
"android": {
"type": "Git",
"repository": {
"type": "GitHub",
"owner": "tadfisher",
"repo": "android-nixpkgs"
},
"branch": "main",
"revision": "5a052c62cdb51b210bc0717177d5bd014cba3df1",
"url": "https://github.com/tadfisher/android-nixpkgs/archive/5a052c62cdb51b210bc0717177d5bd014cba3df1.tar.gz",
"hash": "0w4psgbg4ld937zsycd90d5072bliqq1d3g140s9ln609g3ij3cx"
},
"nixpkgs": {
"type": "Channel",
"name": "nixpkgs-unstable",
"url": "https://releases.nixos.org/nixpkgs/nixpkgs-24.11pre674705.b833ff01a0d6/nixexprs.tar.xz",
"hash": "12cda9rvpgjcsxykbcg5cxjaayhibjjabv6svacjc5n5kpcbx5sf"
}
},
"version": 4
}

View file

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="wifisetup" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>