nix-community / flakelight

Framework for simplifying flake setup [maintainer=@accelbread]
MIT License
177 stars 3 forks source link

Add flakelight-darwin ref in README #13

Closed cmacrae closed 4 months ago

cmacrae commented 4 months ago

Hey @accelbread 👋 I've found flakelight to be a breath of fresh air for composing my configurations, so thanks very much for creating and sharing it :)

These changes include Darwin support, via nix-darwin. It's pretty much a copy & paste of the NixOS code.
It works nicely - I'm using it to configure a MacBook I have. I'd like to be thorough with the tests, however adding an equivalent darwinConfiguration test would require adding nix-darwin to the inputs, which I figured wasn't ideal.

I appreciate updating the default systems to include Darwin may not be desirable, so happy to back that change out if you'd prefer. Also feel free to suggest any changes to wording you'd like to see in the API docs

accelbread commented 4 months ago

Hi, thanks for the contribution!

For a couple of reasons, I think this would be better as a module:

Built-in modules have same access as other modules, so wouldn't need to change much, and would get to add stuff like better defaults for other settings and shorter calling for flakes just using the darwin stuff. The module could then be linked from the flakelight readme.

Can see https://github.com/accelbread/flakelight-rust for example. Would be pretty similar, except could also add the tests file and add the test running to the GH Actions like it is in flakelight. I'll post some code in next comment as well.

If need any help, let me know.

A lot of the above applies to home-manager as well, that probably should have been a separate module.

accelbread commented 4 months ago

The flake.nix for the module repo is likely like this:

{
  description = "Nix-darwin module for flakelite";
  inputs = {
    flakelight.url = "github:nix-community/flakelight";
    nix-darwin.url = "github:LnL7/nix-darwin";
  };
  outputs = { flakelight, nix-darwin, ... }@inputs: flakelight ./. {
    imports = [ flakelight.flakelightModules.flakelightModule ];
    flakelightModule = { lib, ... }: {
      imports = [
        ./flakelight-darwin/darwinModules.nix
        ./flakelight-darwin/darwinConfigurations.nix
      ];
      inputs.nix-darwin = lib.mkDefault nix-darwin;
      systems = [ "x86_64-darwin" "aarch64-darwin" ];
      outputs.tests = import ./tests inputs;
    };
  };
}

And the GH update.yml workflow would look like this (assuming a template is added):

name: Update flake inputs

on:
  schedule:
    - cron: "0 12 ? * TUE"
  workflow_dispatch:

permissions:
  contents: write

jobs:
  update-inputs:
    name: Update inputs
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v4
      - uses: cachix/install-nix-action@master
      - name: Configure git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
      - run: nix flake update --commit-lock-file
      - run: nix flake check --all-systems
      - run: nix eval .#tests
      - run: |
          nix flake check ./templates/darwinSystem --all-systems \
            --override-input flakelight-darwin .
      - run: git push

Calling the flake directly would be possible with above flake, so I'd probably add the stuff from https://github.com/accelbread/flakelight-rust/blob/master/README.md, except swapping options, for the stuff for new api functions. Configured options would probably just mention system.

cmacrae commented 4 months ago

Ah, great! I did actually want to implement this initially as a module, but wasn't entirely sure how to.
Thanks for such a thorough explanation and examples 🙌 I'll switch to implementing this as a module and leave this open just for now, until I've got the module working, in case I run into any blockers, if that's alright? :)

accelbread commented 4 months ago

Yeah sounds good!

Once module is ready, this PR could link it in the readme as well.

cmacrae commented 4 months ago

I created flakelight-darwin :) works nicely!

I was trying to get tests working, but can't figure out how to eval them. Dropping into a Nix REPL and trying to address outputs.tests shows it isn't there. I used your above example. Any ideas?

I'm away right now so don't have access to a computer, but eager to get tests integrated once I'm back

accelbread commented 4 months ago

Hey for the tests you will need to add tests/default.nix; the flake imports tests but it seems the repo doesn't have it.

You'll probably want to make a copy of flakelight's tests, except add a nix-darwin parameter, and remove all the tests not having to do with nixosConfigurations and nixosModules, and change those to darwin.

Also not sure if I'm misreading README, but for autoloading, both darwinConfigurations or darwin should work. The actual name always works and nixDirAliases adds alternate names (since stuff like nixosConfiguations leads to long file names, so allowed nixos for short).

cmacrae commented 4 months ago

Sorry, I should've been more clear: locally on a branch I have created tests/default.nix - I just hadn't tracked it as I hadn't got them working yet :)

It seems that 'outputs.tests' (and thus '.#tests') isn't being exposed somehow. I'm not able to see it in a REPL

As for your comment on autoloading, yes don't worry I'm aware - I use 'darwin' to track my configs. But you're right, that's not clear in the README. I'll add a note to clarify :) thanks for pointing that out

EDIT: I already have this note in the README regarding autoloading. Do you feel it could be a bit more clear?

you can track your darwinConfigurations & darwinModules in directories, named darwin & darwinModules

accelbread commented 4 months ago

Ah, my bad.

Looking at it again, I put the tests thing inside the module; should be:

{
  description = "Nix-darwin module for flakelite";
  inputs = {
    flakelight.url = "github:nix-community/flakelight";
    nix-darwin.url = "github:LnL7/nix-darwin";
  };
  outputs = { flakelight, nix-darwin, ... }@inputs: flakelight ./. {
    imports = [ flakelight.flakelightModules.flakelightModule ];
    flakelightModule = { lib, ... }: {
      imports = [
        ./flakelight-darwin/darwinModules.nix
        ./flakelight-darwin/darwinConfigurations.nix
      ];
      inputs.nix-darwin = lib.mkDefault nix-darwin;
      systems = [ "x86_64-darwin" "aarch64-darwin" ];
    };
    outputs.tests = import ./tests inputs;
  };
}

Hmm, yeah, the README is probably fine; I'd write something like darwinConfigurations (or darwin for short) but its fine as it is. My docs do tend to be wordy, ha.

cmacrae commented 4 months ago

Back home with access to a computer again.
Ahh, okay, I thought that might've been the case, but was setting just tests rather than outputs.tests - reading the API docs again, this makes sense. Got the tests working 👍 thanks very much!

As for the README, I think wordy is good :) I'll try and document it a little clearer

One last question before I switch this PR to just a README update pointing to the project: I noticed you'd included this run directive in the GH workflow example:

      - run: |
          nix flake check ./templates/darwinSystem --all-systems \
            --override-input flakelight-darwin .

I was just wondering what this is :) If it helps with test coverage, I'd like to include it, but wasn't sure I understood what it is/how it should be implemented

accelbread commented 4 months ago

Ah, that just ensures that the template still works, in case changes to the module require template modifications. Also serves as a quick test for modules I haven't written tests for yet.

I would recommend adding a template so users can just run nix flake init -t cmacrae/flakelight-darwin to get going

accelbread commented 4 months ago

Merged; feel free to keep messaging on this thread if you would like. I think GH still notifies.

(I also created a Matrix room a few days ago if thats of interest to you: https://matrix.to/#/#flakelight:nixos.org)

cmacrae commented 4 months ago

Thanks for merging! 🙌 Makes sense regarding the templates, that's a good test 👍 I've added some templates which I agree are probably very helpful

Matrix room certainly interests me too :) thanks for the invite