Creating my first Nix package

There's a new release of Anki that isn't in nixpkgs master yet. I am already running the new release on my computer and all I had to do was to change version and after Nix complained about the hash, it worked. Of course, it wasn't this easy and I had to ask for help on the matrix chatroom (thank you to the kind people there!). It probably wasn't my fault though, this package was written a little werid cause whatever I read about overrides didn't apply to it. The only way to get it working was to download the .nix file and modify it.

The problem is that the package should also build for darwin if I want to submit a PR to nixpkgs and I don't have an apple computer.

Let's first try to get the hash. Nix has a convenient nix-prefetch-url tool for this. But, it didn't work for me for whatever reason. I tried it with the old release of Anki and the hash from nix-prefetch-url didn't match the hash that was in the original anki.nix.

Not correct

I got a little hacky instead of figuring out the correct way to use this tool. I changed darwin to linux so that it would fetch the dmg when it tried to build it for my computer.

:s/darwin-x86_64/linux
looks like i got the hash
gotta get the ARM too

Okay, that was easy. But how do I know if it actually works on darwin? I asked for help on the matrix room and someone pointed me to OSX-KVM. It looked kinda hacky and I remember the hoops I had to jump through to install hackintosh on my computer when I was in high school many years ago. But the README said that AWS offers EC2 with darwin. It must be my lucky day cause I finally get to use those sweet AWS credits that I don't know what else to do with! Well, no, they do have darwin EC2 but won't let you use them unless you surrender your credit card cause, you see, free credits only work for overpriced shit that should be free regardless.

After some digging around, I found this which seemed simple enough. But it didn't work so I tried this. That didn't work either so I went back to this which is the popular one but doesn't acknowledge Nix which is why I tried the other two first (cause things made for Linux usuallly don't work OOTB on Nix).

okay, this one did work
now i wait

It took so fucking long that I gave up after waiting till 5 in the morning and went to sleep, but good morning and here I am installing Nix on my brand new apple computer.

I copied anki.nix over and installed it. It worked without me having to change anything. But, it won't run.

:(

I tried installing the old package and that wouldn't run either so that makes me think it has something to do with my fake apple computer rather than my package. It does install so I will take that as a win? Now we fork.

Apparently you need to run nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD" to make sure your changes don't break anything else, but this takes a long time cause it downloads 3.99 GiB of source??

That's not the worst part cause I have decent internet. It's what it does with all that code. God knows what it's going to compile and how many hours it'll take. I'm gonna get my PR ready while I wait for this to finish.

It did build on x86_64-darwin, check.

sandbox = false on my fake apple computer and there are no tests for this package. No tests no fail.

Hopefully, this will be true soon.

Yes, tested thoroughly and it works on my machine. CONTRIBUTING.md doesn't say much about simply bumping versions. I guess it's largely for people who write new packages and that's not me.

Still waiting for this oh my god save my sanity. Is this really important?

Oh look it said something! Something about something going well. It said it's gonna stop and I think it did and another command was invoked. Upon closer look, the output is just git saying that the automatic merge works which I already knew, but I guess it's important since my branch is already 55 commits behind master. The second command is identical to the first one except for the --meta flag. I wonder what that does and how long it'll take compared to the other one. For comparison:

First:
$ nix-env --extra-experimental-features no-url-literals --option system x86_64-linux -f <nixpkgs> --nix-path nixpkgs=/home/yt/.cache/nixpkgs-review/rev-24ccd282cf2a78e87523149a562140b3f704ce7e/nixpkgs nixpkgs-overlays=/tmp/nix-shell-48210-0/tmpkd7dz7pl -qaP --xml --out-path --show-trace --no-allow-import-from-derivation

Second:
$ nix-env --extra-experimental-features no-url-literals --option system x86_64-linux -f <nixpkgs> --nix-path nixpkgs=/home/yt/.cache/nixpkgs-review/rev-24ccd282cf2a78e87523149a562140b3f704ce7e/nixpkgs nixpkgs-overlays=/tmp/nix-shell-48210-0/tmpkd7dz7pl -qaP --xml --out-path --show-trace --no-allow-import-from-derivation --meta

Okay umm yeah that's what I wanted to do.

And it dropped me to a shell, but I don't know what I'm supposed to do here. There are some files in here with some information about my package. Everything looks fine to me.

[nix-shell:~/.cache/nixpkgs-review/rev-...]$ ls
attrs.nix  logs  nixpkgs  report.json  report.md  results

[nix-shell:~/.cache/nixpkgs-review/rev-...]$ cat report.md 
## `nixpkgs-review` result

Generated using [`nixpkgs-review`](https://github.com/Mic92/nixpkgs-review).

Command: `nixpkgs-review`

---
### `x86_64-linux`
<details>
  <summary>:white_check_mark: 1 package built:</summary>
  <ul>
    <li>anki-bin</li>
  </ul>
</details>

[nix-shell:~/.cache/nixpkgs-review/rev-...]$ cat attrs.nix 
{
  x86_64-linux = [
    "anki-bin"
  ];
}

I can of course launch anki from here and it works as expected. I think my PR is ready for review! Well, actually, anki launched the one already installed on my computer and not from this ephemeral shell. The one that was just built is in ./results/anki-bin-x86_64-linux/bin/anki. Regardless, this one works just as well.

I submitted the PR #361858. I hope that it works and I won't waste anyone's time cause I know I may have messed up somewhere.

UPDATE: Oh well, that didn't work out so well. But at least, nothing was wrong with my PR. The PR that was merged was exactly the same as mine. The worst part is that it was submitted FOUR days ago. I am so fucking stupid.

UPDATE 2: I did start to work on anki sine it's still out-of-date. This was somewhat more difficult cause this package builds from source, but I had to have a merged PR for the sake of this blog post. I should have been clear about the difference between anki and anki-bin from the start to avoid any confusion. That was a very stupid mistake on my part cause nixpkgs automatic builder relies on the package name you put in your commit message. Here's a quick diff of the changes I made for the main anki package.

diff --git a/pkgs/games/anki/default.nix b/pkgs/games/anki/default.nix
index d9a4984d3d79..8464483eb536 100644
--- a/pkgs/games/anki/default.nix
+++ b/pkgs/games/anki/default.nix
@@ -31,14 +31,14 @@
 
 let
   pname = "anki";
-  version = "24.06.3";
-  rev = "d678e39350a2d243242a69f4e22f5192b04398f2";
+  version = "24.11";
+  rev = "87ccd24efd0ea635558b1679614b6763e4f514eb";
 
   src = fetchFromGitHub {
     owner = "ankitects";
     repo = "anki";
     rev = version;
-    hash = "sha256-ap8WFDDSGonk5kgXXIsADwAwd7o6Nsy6Wxsa7r1iUIM=";
+    hash = "sha256-pAQBl5KbTu7LD3gKBaiyn4QiWeGYoGmxD3sDJfCZVdA=";
     fetchSubmodules = true;
   };
 
@@ -53,7 +53,7 @@ let
 
   yarnOfflineCache = fetchYarnDeps {
     yarnLock = "${src}/yarn.lock";
-    hash = "sha256-Dbd7RtE0td7li7oqPPfBmAsbXPM8ed9NTAhM5gytpG8=";
+    hash = "sha256-4KQKWwlr+FuUmomKO3TEoDoSStjnyLutDxCfqGr6jzk=";
   };
 
   anki-build-python = python3.withPackages (ps: with ps; [ mypy-protobuf ]);
@@ -135,8 +135,6 @@ python3.pkgs.buildPythonApplication {
     ./patches/disable-auto-update.patch
     ./patches/remove-the-gl-library-workaround.patch
     ./patches/skip-formatting-python-code.patch
-    # Also remove from anki/sync-server.nix on next update
-    ./patches/Cargo.lock-update-time-for-rust-1.80.patch
   ];
 
   inherit cargoDeps yarnOfflineCache;
@@ -224,7 +222,7 @@ python3.pkgs.buildPythonApplication {
     astroid
   ];
 
-  # tests fail with to many open files
+  # tests fail with too many open files
   # TODO: verify if this is still true (I can't, no mac)
   doCheck = !stdenv.hostPlatform.isDarwin;
 

And for the anki-sync-server package:

diff --git a/pkgs/games/anki/sync-server.nix b/pkgs/games/anki/sync-server.nix
index a312887f5d27..a68e615c3d91 100644
--- a/pkgs/games/anki/sync-server.nix
+++ b/pkgs/games/anki/sync-server.nix
@@ -12,10 +12,6 @@ rustPlatform.buildRustPackage {
   pname = "anki-sync-server";
   inherit (anki) version src cargoLock;
 
-  patches = [
-    ./patches/Cargo.lock-update-time-for-rust-1.80.patch
-  ];
-
   # only build sync server
   cargoBuildFlags = [
     "--bin"

This package is marked as broken on darwin. Let me try and see if my build works on darwin.

# Reported to crash at launch on darwin (as of 2.1.65)
    broken = stdenv.hostPlatform.isDarwin;

Yeah, no.

Here's another PR for today: #361951