Code Monkey home page Code Monkey logo

twist.nix's Introduction

Twist Nix Library

https://github.com/emacs-twist/twist.nix/actions/workflows/test.yml/badge.svg

Status

This repository is currently in alpha state. It has finished basic functionalities, but there is still room for improvement in API, and it completely lacks proper documentation:

  • Functionalities: Good. The author builds his own config with twist, and the config is already almost as capable as the previous version which used straight.el. Additionally, NoMake is used in some packages (but it is being replaced with Rice). The basic use cases of twist.nix are covered.
  • API: Unstable. It is mostly stable, but options may undergo changes in the future.
  • Documentation: Poor. There is a branch. I will rework on it after I gain more confidence with the API.

Introduction

Twist.nix is a Nix library for building Emacs configurations with packages. It is an infrastructure for configuration (solely with this library) and package development (with nomake).

This repository is an integral component of emacs-twist project. The goal of the project is to provide an alternative Emacs ecosystem that uses Nix. It is experimental, but also aims to be useful.

There are several other components under development. See the following table for comparison with corresponding options:

Twist componentDescriptionCounterpart
twist.nix (this repo)Build machinerypackage-build
twist.el combined with nix3.elEmacs package managerstraight.el, borg, etc.
​+nomake+​ ricePackage developmentcask

As a Nix library, Twist.nix is also an alternative to the Emacs wrapper (i.e. emacsWithPackages) on NixOS. Twist.nix depends on Nix utility libraries, but it does not depend on the wrapper.

The biggest difference betweeen twist and the wrapper is that twist is capable of building packages from upstream source repositories. On the contrary, the wrapper fetches pre-built package archives from for most ELPA and MELPA packages, which means it indirectly depends on package-build for MELPA packages.

Here building a package means mapping files into a flat directory, which takes little time. By being smarter in building packages from sources, twist has an advantage in working around existing packages and adding custom packages, like straight.el. It also allows usage for package development, which is what package-build does to cask.

With twist, it is easier to lock and update individual packages, because it tracks package versions in flake.lock. Twist.el, the package management frontend for Twist, should hopefully provide an experience on par with straight.el.

Twist can discover and build packages from the following sources:

  • MELPA recipes (Git)
  • ELPA external/core packages (Git)
  • ELPA/MELPA archives (single elisp files and tarballs)
  • EmacsMirror (Git)

To add custom packages to your configuration, you only have to define MELPA recipes and commit them to your repository. It can already build configurations with a few hundreds of packages from various registries (see examples).

Credits

Twist is a Nix re-implementation of package-build and replicates its build logic. It is also heavily influenced by the Emacs wrapper in nixpkgs, though twist was written from scratch and different in implementation.

twist.nix's People

Contributors

akirak avatar dependabot[bot] avatar jordanisaacs avatar luigipiucco avatar warashi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

twist.nix's Issues

How to work around upstream archives being removed

As reported in #159 (comment), a configuration fails to build if one of the source archives tracked in archive.lock is removed from the original location. Using archives is generally safer for packages on GNU ELPA, as some packages on the registry are built in specific ways.

Apparently, when a package on GNU ELPA is updated, the previous version is compressed with lzip, and the initial uncompressed archive is removed. This breaks reproducibility of this project. Also, .tar.lz is not natively supported by Nix flakes, so it wouldn't fit the current system of archive.lock anyway.

I don't come up with a solution right now. Perhaps some extra infrastructure will be needed? I wouldn't be happy with it, but the issue needs to be addressed to make this project usable.

Documentation on native-comp

I am a bit confused on native-comp support.
Could you provide documentation on the state of nativ-comp support in twist.nix?

It would be really nice to be able to compile all packages using native-comp.

How to handle source files that can't be processed with buitlins.readFile

At present, there are two ways in twist to read the main file of a package to parse the headers:

  • Directly using builtins.readFile. This is not IFD, and it is effective when mainIsAscii or defaultMainIsAscii (#167) is set to true.
  • Take the first 1500 characters of the file using head command and read the output using buitlins.readFile. This is IFD, which we want to avoid for package development.

The latter is required because buitlins.readFile can't process a file that contains non-ASCII characters. For example, org-ql.el contains control characters, so org-ql can't be added as a dependency without IFD.

This is inconvenient and makes rice unusable for many packages.

A workaround would be a generation approach: persist the header data to a file and read from it when available. We can probably save the metadata of all packages to a JSON file in the lock directory during lock. A downside of this approach is that the lock directory will contain another file, which will increase the repository size. Bonus is the user can see the diffs of dependencies and the build process may be faster.

Test fails due to addition of bbdb-notmuch.el to bbdb

CI failed due to a recent addition of a library to bbdb package:

    > In toplevel form:
   > bbdb-notmuch.el:27:2: Error: Cannot open load file: No such file or directory, notmuch-show

bbdb-notmuch.el is a recent addition to bbdb package, which is found in this diff.

This is likely a configuration problem. I will fix the test case later.

Package from inventory of type archive somehow evaluates elpa.nix on CI

This is a really strange one. I ran into the issue of this working locally, but not on CI.

It somehow finds the package in elpa (I guess, since it enters elpa.nix) on CI, but not locally. So the locked file is not correct. Locally the package is not found in the top inventories so it reaches the archive and hence when running nix run .#lock and nix run .#update it generates the archive.lock instead of adding it to the flake.lock.

If I put the archive above GNU ELPA it works on CI as well.

I have the current defined inventory:

[
  {
    name = "custom";
    type = "melpa";
    path = ../recipes;
  }
  {
    name = "gnu";
    type = "elpa";
    path = self.inputs.gnu-elpa.outPath + "/elpa-packages";
    core-src = emacsSrc;
    auto-sync-only = true;
  }
  {
    name = "melpa";
    type = "melpa";
    path = self.inputs.melpa.outPath + "/recipes";
  }
  {
    name = "nongnu";
    type = "elpa";
    path = self.inputs.nongnu-elpa.outPath + "/elpa-packages";
  }
  {
    name = "gnu-archive";
    type = "archive";
    url = "https://elpa.gnu.org/packages/";
  }
]

and archive.lock:

{
...
  "jinx": {
    "archive": {
      "narHash": "sha256-NA2ulVZuhetCYxwvUgOv8nDAdC9LYFDpj7O42pD/wyI=",
      "type": "tarball",
      "url": "https://elpa.gnu.org/packages/jinx-0.6.tar"
    },
    "inventory": {
      "type": "archive",
      "url": "https://elpa.gnu.org/packages/"
    },
    "packageRequires": {
      "compat": "29.1.4.0",
      "emacs": "27.1"
    },
    "version": "0.6"
  },
...
}

Here is the CI build:
https://github.com/terlar/emacs-config/actions/runs/4751840604/jobs/8441454659
and the source commit:
https://github.com/terlar/emacs-config/tree/0f1615ddb22b81d9f61a22166cbe95e8a8819fcf

Did you run into something like this?

Add a rationale to the readme

The goal of this project is to provide a framework with which the user can lock and update individual packages in his/her config. The primary reason behind this goal is security, and the issue has been discussed several times:

(There are more reddit threads on the topic, and you can easily reach them via DuckDuckGo.)

I haven't described it in the README yet. I find it difficult to concisely describe the reason, but I will have to do that.

cbowdon provides the following checklist, and Twist would help with the first criteria:

  • reviewing every single update prior to installing (hardly feasible)
  • code signing (not sure if package.el supports this though)
  • waiting before updating, in hopes that an attack would be discovered before you install
  • installing fewer packages to limit potential exposure
  • generic measures like not running as root and having endpoint protection on Windows

Allow easily share and compose Emacs Lisp package overrides

To build some Emacs Lisp packages in Nix, Twist requires the user to override the derivations (example). At present, the API does not allow easily sharing overrides between multiple users. Ideally, there should be a community-maintained repository for an overlay, and the user should be able to define his/her own overrides in addition to that.

This is applied to both Emacs configurations and nomake. NoMake is a framework for linting and developing Emacs Lisp packages. If a package contains a dynamic module or depends on another package that requires one, it will require an override.

Some packages also have missing dependencies (which are upstream issues), which can be workaround like this.

  • Provide workarounds as a separate repository, in a form of overlay.
  • Refine the configuration API to allow composition of overrides, if necessary. not needed after #128

Fix handling of :renames from elpa-packages when the destination is empty

Upon updating magit-section to my lockDir, I have captured its build failure error.

log:

sandbox setup: bind mounting '/nix/store/zzx3hx8awlz31spdj8ms14wd98v1yfpb-db-5.3.28' to '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv.chroot/nix/store/zzx3hx8awlz31spdj8ms14wd98v1yfpb-db-5.3.28'
sandbox setup: closing leaked FD 3
sandbox setup: closing leaked FD 4
sandbox setup: closing leaked FD 5
sandbox setup: closing leaked FD 6
sandbox setup: closing leaked FD 7
sandbox setup: closing leaked FD 8
sandbox setup: closing leaked FD 9
sandbox setup: closing leaked FD 10
sandbox setup: closing leaked FD 11
sandbox setup: closing leaked FD 12
sandbox setup: closing leaked FD 13
sandbox setup: closing leaked FD 14
sandbox setup: closing leaked FD 15
sandbox setup: closing leaked FD 16
sandbox setup: closing leaked FD 17
sandbox setup: closing leaked FD 20
building '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv'...
building of '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv^out' from .drv file: got EOF
building of '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv^out' from .drv file: woken up
building of '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv^out' from .drv file: build done
killing process 658448
builder process for '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv' finished
killing all processes running under uid '30001'
lock released on '/nix/store/531l4la167vn9zql2qsc4zimssw0ld17-emacs-git-20231211-0-magit-section-3.3.0.50-git.lock'
lock released on '/nix/store/l7kfqgmni29g9l1flrzrg9vzhdlxccdl-emacs-git-20231211-0-magit-section-3.3.0.50-git-info.lock'
building of '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv^out' from .drv file: done
error: builder for '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv' failed with exit code 1;
       last 8 log lines:
       > Running phase: unpackPhase
       > unpacking source archive /nix/store/bkkz28wc6ppz7dpwzxkzg9pwxxrg3xff-source
       > source root is /build/build
       > Running phase: patchPhase
       > Running phase: buildPhase
       > mv: missing destination file operand after '.'
       > Try 'mv --help' for more information.
       > /nix/store/d4jf1cbbk494zwgbqz31pxgigpsbh6w2-stdenv-linux/setup: line 131: pop_var_context: head of shell_variables not a function context
       For full logs, run 'nix log /nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv'.
building of '/nix/store/7g5m5xnvw6rw2mcw8h0qdz5i5vbzx9k4-emacs.drv^out' from .drv file: waitee 'building of '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv^out' from .drv file' done; 3 left
building of '/nix/store/i0g1sx3clfcrdak3bflys5dwzh2p8g2d-elisp-digest.json.drv^out' from .drv file: goal destroyed
building of '/nix/store/hhzmvsganhs5ph7piir8s3va1rfmj2nj-elisp-packages.drv^out' from .drv file: goal destroyed
building of '/nix/store/dayjk42pigrn6147cny98g222r8d6rr8-emacs-git-20231211-0-nix-mode-1.5.0.drv^out' from .drv file: goal destroyed
building of '/nix/store/7g5m5xnvw6rw2mcw8h0qdz5i5vbzx9k4-emacs.drv^out' from .drv file: woken up
building of '/nix/store/rlfar4f97kg5f5674rfk6cbl8zcjpdg3-emacs-git-20231211-0-magit-section-3.3.0.50-git.drv^out' from .drv file: goal destroyed
building of '/nix/store/7g5m5xnvw6rw2mcw8h0qdz5i5vbzx9k4-emacs.drv^out' from .drv file: all inputs realised
building of '/nix/store/7g5m5xnvw6rw2mcw8h0qdz5i5vbzx9k4-emacs.drv^out' from .drv file: done
building of '/nix/store/7g5m5xnvw6rw2mcw8h0qdz5i5vbzx9k4-emacs.drv^out' from .drv file: goal destroyed
error: 1 dependencies of derivation '/nix/store/7g5m5xnvw6rw2mcw8h0qdz5i5vbzx9k4-emacs.drv' failed to build

It seems that renamePhase failed during the execution of mv docs/*.* ..

I am at a loss regarding this, how should I resolve it?

As additional info, the flake of magit-section:

    "magit-section": {
      "flake": false,
      "locked": {
        "lastModified": 1704784205,
        "narHash": "sha256-qiQQ1nKm+f1Hx9cipHSmxP6ED2SntnWx/qm1erUS1t4=",
        "owner": "magit",
        "repo": "magit",
        "rev": "c3b7fd7dc43dd87468a86aef3d59576ad79fbc16",
        "type": "github"
      },
      "original": {
        "owner": "magit",
        "repo": "magit",
        "type": "github"
      }
    },

What is the procedure to updating the lock?

I am still not sure what the procedure is to update the lock.

We do have nix run .#lock --impure and nix run .#update --impure. But neither seems to update packages unless you remove the current lock information first. It just updates packages that are modified, but don't update to later versions for the ones that exist.

E.g. this works:

rm lock/*
nix run .#lock --impure

Is this the procedure or should these other helper commands work in some way?

Expose build function to make custom derivations

It would be nice if the build function was exposed so one could make custom derivations:
https://github.com/emacs-twist/twist.nix/blob/master/pkgs/emacs/build/default.nix

The use case is that I want to build my configuration and not install it as a package but still would like to byte compile it. Perhaps the correct thing is to make packages out of this not sure, but at least that is what I am doing currently.

I have achieved this by a bit of a hack using the trivialBuild instead:

final: prev: {
  emacsEnv =
    (final.emacsTwist {
      emacsPackage = final.emacsPgtkNativeComp.overrideAttrs (_: {version = "29.0.50";});

      initFiles = [(final.tangleOrgBabelFile "init.el" ./init.org {})];

      lockDir = ./lock;
      inventories = import ./nix/inventories.nix {
        inherit self;
        emacsSrc = final.emacsPgtkNativeComp.src.outPath;
      };

      inputOverrides = import ./nix/inputOverrides.nix;
    })
      .overrideScope' (tfinal: tprev: {
        elispPackages = tprev.elispPackages.overrideScope' (
          prev.callPackage ./nix/packageOverrides.nix {inherit (tprev) emacs;}
        );
      });

  emacsConfig = let
    emacs = let
      self =
        final.emacsEnv
        // {
          inherit (final.emacsEnv.emacs) meta;
          overrideAttrs = _: self;
        };
    in
      self;

    attrs = nixpkgs.lib.optionalAttrs (self ? lastModifiedDate) {
      version = nixpkgs.lib.substring 0 8 self.lastModifiedDate;
    };
  in
    (prev.emacsPackagesFor emacs).callPackage ./. attrs;
}

Then later it is used in custom packages:

trivialBuild {
  pname = "config-init";
  inherit version;

  src = lib.sourceByRegex ./. ["init.org" "lisp" "lisp/.*.el$"];

  buildPhase = ''
    emacs --batch --quick \
      --load org \
      *.org \
      --funcall org-babel-tangle

    mkdir -p .xdg-config
    ln -s $PWD .xdg-config/emacs
    export XDG_CONFIG_HOME="$PWD/.xdg-config"

    emacs -L . --batch --eval '(setq byte-compile-error-on-warn t)' -f batch-byte-compile *.el
  '';
}

Automatically generating a package-quickstart

To speed up Emacs I've been experimenting with having a derivation that generates package-quickstart via a derivation. I currently do this (given an Emacs package with all packages on path):

trivialBuild {
  pname = "config-package-quickstart";
  inherit version;

  dontUnpack = true;

  buildPhase = ''
    emacs --batch --quick \
      --load package \
      --eval '(setq package-quickstart-file "package-quickstart.el")' \
      --eval '(setq package-quickstart t)' \
      --funcall package-quickstart-refresh
  '';
}

I think package-quickstart integration into the nix Emacs Eco-system could be beneficial. Since it is a build-time thing that is dependent on it's inputs.

How to install package dependent on external artifact like `mu4e`

Recently, I am trying to incorporate mu4e into my configuration, but I don't know how build it by Twist.

The building of mu4e is dependent on the artifacts of mu.

For example, as build by nixpkgs elispPackages.

https://github.com/NixOS/nixpkgs/blob/b9c6b11eafbb693c3d9d820bea1636a51c87b2a5/pkgs/applications/editors/emacs/elisp-packages/manual-packages/mu4e/default.nix#L7-L33

I attempted to independently build its Emacs Lisp portion, but it appears that there are many missing components that I am unsure how to address. For example, mu-guile and a mu4e-config.el generated from the template.

https://github.com/djcb/mu/blob/62f0a9a902031d172e03e8caaaad013a1798749f/mu4e/meson.build#L19-L27

I am interested in understanding the methods available for extracting the mu output and integrating it with twist. Alternatively, are there other reproducible build strategies?

Support ELPA packages with :url nil

OT
So I'm finally getting around to explore this and doing my own take on using this and I'm avoiding emacsmirror.
/OT

When I try to run the emacsTwist function on my configuration and I guess it tries to look up a package from the elpa inventory it fails when trying to find cycle-quotes. I checked the elpa-packages file and noticed ("cycle-quotes" :url nil) and I'm guessing that is the case why it is not found.

Reading at the top of this file I see:

;; For KIND :url URL is the URL of the remote git repository that we want
;; to track, or `nil' if there is no upstream, i.e., if development of this
;; package takes place directly on the `externals/<name>' branch of the
;; `elpa.git' repository.  For packages of KIND :core URL must be a list of:
;;    STRING = A file-name to copy from Emacs repo.
;;    (STRING STRING) = A file-name to copy renamed from Emacs repo.

I'm guessing :url nil, e.g. looking inside externals/<name> is not currently supported or do you have any other idea why it would fail finding this specific package?

Persist fails to build

@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/9gg7ryydfkhvmh0vl2aalq5h3jl57igs-source
source root is /build/build
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "buildPhase" }
building
Package autoload is deprecated
  SCRAPE   .
  INFO     Scraping files for persist-autoloads.el... 
  INFO     Scraping files for persist-autoloads.el...done
persist.texi:55: raising the section level of @section which is too low
/nix/store/gv2cl6qvvslz5h15vqd89f1rpvrdg5yc-stdenv-linux/setup: line 146: pop_var_context: head of shell_variables not a function context

when installing my setup

emacs-overlay emacs versions problematic together with emacs wrapper

Given how emacs-overlay sets the versions and how the code relies on version here:

You will get invalid symlinks as the version 20221216.0 will be symlinked but the actual value should be 30.0.50 for example.

I know before you had the check that verified the version and now that is removed. Just curious if you are aware of this one or if we should document the workaround .overrideAttrs (_: {version = "30.0.50";}) for example. Best would be if the actual version was available somewhere.

How to tackle issue with commits seemingly "disappearing" from ELPA

Hello, I am sorry to raise this here as it is per se not an issue with twist.nix. However I figured you might have run into the same thing and found a solution. Or perhaps there is some weird thing making me run into this but not others.

The issue is with tracking the flake input git+https://git.savannah.gnu.org/git/emacs/elpa.git?ref=main. I have builds working at some revision and next time I try to re-run it, it fails with this kind of message:

       error: Cannot find Git revision 'd5ea227a3729357b6c4c09bd033db9445fa34048' in ref 'main' of repository 'https://git.savannah.gnu.org/git/emacs/elpa.git'! Please make sure that the rev exists on the ref you've specified or add allRefs = true; to fetchGit.

If I search the ELPA git repo, I can find the commit:
https://git.savannah.gnu.org/gitweb/?p=emacs/elpa.git;a=commit;h=d5ea227a3729357b6c4c09bd033db9445fa34048

It also seems to still be associated with the main branch (* elpa-packages (window-tool-bar): New core package):
https://git.savannah.gnu.org/gitweb/?p=emacs/elpa.git;a=shortlog;h=refs/heads/main

I only see this behavior with ELPA and it is causing failures for pipelines that worked in the past and only way I found to move forward was to update the input, but it keeps happening.

Feel free to close, in the meantime I will keep investigating...

Make it work with the latest version of Emacs

The latest version of Emacs fails to start (emacs-twist/examples#29 (comment)):

Error using execdir /nix/store/3m4jv6pk4hgjmyrr9g5pchf8h2g5v23j-emacs/bin/:
emacs: /nix/store/3m4jv6pk4hgjmyrr9g5pchf8h2g5v23j-emacs/bin/../native-lisp/28.1.91-002d2166/preloaded/window-0d1b8b93-7ef4271a.eln: cannot open shared object file: No such file or directory

This is due to how twist organizes artifacts. I will work on it later.

API Request Rate Limiting

Hi! I was just wondering if it's on the roadmap to update twist such that it doesn't make so many API requests to github? I have an api key set up which helps some but I still get rate limited. Using a vpn is my current workaround but that doesn't seem ideal.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.