Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC 0132] Meson Builds Nix #132

Draft
wants to merge 61 commits into
base: master
Choose a base branch
from
Draft
Changes from 43 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
9714f41
Use Meson to build Nix
AndersonTorres Aug 25, 2022
85759af
Modify feature attribute
AndersonTorres Aug 26, 2022
003393e
Small clarification
AndersonTorres Aug 26, 2022
3e0be88
Reformulates the alternatives section
AndersonTorres Aug 26, 2022
d4497fd
Answer new drawback
AndersonTorres Aug 26, 2022
18c732d
Typo
AndersonTorres Aug 26, 2022
a843fb1
Rewording
AndersonTorres Aug 26, 2022
0bf382d
Unfill the paragraphs
AndersonTorres Aug 26, 2022
6c3f5d2
Rename RFC file
AndersonTorres Aug 26, 2022
bd4bfe9
Small punctuation fixes
AndersonTorres Aug 27, 2022
e1a0620
Rewording
AndersonTorres Aug 27, 2022
0060937
Add usage example
AndersonTorres Aug 27, 2022
265020c
Add references
AndersonTorres Aug 27, 2022
b7b859b
Improvements on the wording
AndersonTorres Aug 27, 2022
3eea629
Reword the reference to Nix
AndersonTorres Aug 31, 2022
8650a36
Typo
AndersonTorres Aug 31, 2022
ab2dec1
Typo
AndersonTorres Aug 31, 2022
6980048
Reword a paragraph
AndersonTorres Aug 31, 2022
b58c8b6
Reword the detailed design
AndersonTorres Aug 31, 2022
17069b6
Reword the examples
AndersonTorres Aug 31, 2022
9821ad2
Add a paragraph about the current (quasi-) autotools
AndersonTorres Aug 31, 2022
ab42c2f
Reword commentary about transition
AndersonTorres Aug 31, 2022
3111e49
Reword alternatives
AndersonTorres Aug 31, 2022
9d89692
Reword commentary about Cmake
AndersonTorres Aug 31, 2022
dca9276
Remove commentary about backport
AndersonTorres Aug 31, 2022
38f3c44
Bring more items to future work
AndersonTorres Aug 31, 2022
714457b
Add references
AndersonTorres Aug 31, 2022
894946b
Reorder references
AndersonTorres Sep 1, 2022
72dec20
Reword motivation section
AndersonTorres Sep 1, 2022
6d8f6bd
Reword paragraph
AndersonTorres Sep 1, 2022
94eb5c7
Rework examples and interactions
AndersonTorres Sep 1, 2022
934555d
Rework drawbacks
AndersonTorres Sep 1, 2022
16e2033
Rework alternatives
AndersonTorres Sep 1, 2022
8dadf6a
Some fixups
AndersonTorres Sep 5, 2022
587d349
Include some more examples of build systems
AndersonTorres Sep 9, 2022
86bc4bc
Reorder references
AndersonTorres Sep 9, 2022
9ad0874
Reword cmake subsection
AndersonTorres Sep 9, 2022
5e621af
Move paragraphs about Meson and Ninja to detailed design section
AndersonTorres Sep 9, 2022
3de1372
Remove unneeded paragraph
AndersonTorres Sep 10, 2022
c5a9e48
Include Bazel as alternative
AndersonTorres Sep 10, 2022
3f1182c
Reword paragraph
AndersonTorres Sep 10, 2022
e361407
Punctuation
AndersonTorres Sep 10, 2022
a228c6f
More references
AndersonTorres Sep 10, 2022
aef0827
Reword
AndersonTorres Sep 11, 2022
212bf63
Typo
AndersonTorres Sep 11, 2022
4df4944
Reword
AndersonTorres Sep 11, 2022
0a41323
A bit more about Bazel
AndersonTorres Sep 11, 2022
e071944
add feature: out-of-source build
AndersonTorres Oct 6, 2022
d8904e9
Typos, typos and more typos
AndersonTorres Nov 11, 2022
680e6b8
Link to the now free-as-in-beer book from the creator of Meson
AndersonTorres Nov 11, 2022
0430b71
Another typo
AndersonTorres Nov 11, 2022
61ad7c4
Add shepherds
edolstra Nov 17, 2022
afca581
add cross-compilation support to the criteria of evaluation of altern…
AndersonTorres Aug 12, 2023
9bb63e0
Typo
AndersonTorres Feb 25, 2024
e6b4e05
Reword examples
AndersonTorres Feb 25, 2024
719562c
Add some more advantages for Bazel
AndersonTorres Feb 25, 2024
e4715c4
Reword and add more disadvantages for Bazel
AndersonTorres Feb 25, 2024
f37cedd
More references
AndersonTorres Feb 25, 2024
5b0a734
Reword Google company name
AndersonTorres Feb 25, 2024
56c86fa
Add Prior Art section
AndersonTorres Feb 25, 2024
b03e5b6
Rewords and comparisons
AndersonTorres Feb 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 216 additions & 0 deletions rfcs/0132-meson-builds-nix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
---
feature: meson_builds_nix
start-date: 2022-08-25
author: Anderson Torres
co-authors: @p01arst0rm
shepherd-team:
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
shepherd-leader:
related-issues:
---

# Summary
[summary]: #summary

Use meson as an alternative build system for Nix.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

# Motivation
[motivation]: #motivation

Currently, the Nix evaluator and its companion toolset generated from the Nix source tree are built using the typical `./configure` shell script that relies on autoconf, along with standard GNU Make utility.

Over time, this build system has been modified to keep up with the development needs of Nix project. However, it has reached a state where the build system became clunky and plastered, hard to understand and modify, consequently making improvements to the project as a whole very difficult.

In particular, many changes have been introduced that impact compatibility outside Linux and NixOS niches. These issues can hinder development on other platforms, including but not limited to Unix-like systems.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide examples to changes that affect compatibility? Are there issues you can link?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asymmetric bunch of stuff; some of the issues were to do with hardcoded library dependencies that are set up differently on *BSD, the wrong versions of some headers were included on BSD (sometimes headers have names that differ). there were also issues with locating external components iirc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but I was suggesting these issues could be mentioned in the RFC itself, possibly with references!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats a fair point, but more detailed info of these issues would require someone to attempt building nix on multiple different platforms, and take notes. not saying its a bad idea/shouldnt be done but it would take some time

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @asymmetric, this needs some concrete examples for the point to really stand

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NixOS/nix#6991 << example :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and that PR has a very illuminating comment from me:

The first commit is just #5145 and again #6015

Previous efforts that actually used a different tactic include:
#5185
#6005

Issues include:
#5140
#5184
#5781

This issue has been separately discovered by a variety of people building nix for use as a packager manager but not as an OS. Meson would solve the problem for free, as part of providing functionality that Make doesn't have and is apparently needed.


In light of this state of things, we propose a novel alternative to the current building infrastructure.

We expect to accomplish, among other goals,

- better code structuring and documentation;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see why that could be so in the long run, but it's an indirect and non totally obvious consequence. It's probably worth explaining a bit more

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe @p01arst0rm can provide more tangible evidence for it.

Copy link

@p01arst0rm p01arst0rm Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meson allows standardised methods of creating targets; dependencies, libraries, binary files, macros, etc. due to design these blocks are incredibly easy to understand in their design, and the implementation of each is standard no matter the project; each of these implementations is heavil documented, explain how and how not to format these targets. meson code is well known for being self documenting;- not in a "read the code and you'll understand it maybe", more like every single piece of standard meson code explains exactly what its doing and includes a short essay of exceptional documentation regarding its use. this style is so influential that competing build systems are already attempting to follow in mesons' footsteps. the only time things maaay get confusing are when you start creating non standard targets to do weird things (known as custom_targets). good meson code should have as little of these as possible.

tldr meson code easy to read and lots of docs means you're never looking at a meson build command confused about what it does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I trust you on that, but I'd rather see this written in the RFC than hidden in a comment thread ;)

- improved support across platforms, especially outside NixOS and Linux
- shorter build times;
- improved testing;
- more reasonable dependency management;
- an overall improved user experience.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

# Detailed design
[design]: #detailed-design

## What is Meson?

Meson is an open source, multiplatform build system crafted to be fast, correct and user-friendly.

According to its main site,

> The main design point of Meson is that every moment a developer spends writing or debugging build definitions is a second wasted. So is every second spent waiting for the build system to actually start compiling code.

Among its features, we highlight:

- user-friendly non-Turing complete DSL
- very readable Python-esque syntax and functional, stateless semantics
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
- multiplatform support
- among operating systems: Linux, Apple MacOS, Microsoft Windows NT
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
- among programming environments: GCC, Clang, Xcode, Visual Studio etc.
- among programming languages: C, C++, D, Fortran, Java, Rust etc.
- supports command customization
- cross compilation
- many useful modules included (pkg-config, filesystem inspection, internationalization etc.)
- Comprehensive documentation
- including tutorials, reference manuals and real world projects using it

## What is Ninja?

Ninja is a small, speed-focused build tool that fills a similar role of Unix `make` or its GNU counterpart `gmake`.

Its main feature is a low-level approach to build description. Ninja is bare-bones and constrained by design, having only the necessary semantics to describe build dependency graphs, relegating decision-making to superior tools like Meson or CMake. Where other build systems act like high level languages, Ninja acts like an assembly.

Albeit Ninja DSL is human-readable, it is not convenient to be manually written by human beings. As said before, Ninja is commonly used in tandem with other, higher-level build system in a two-pass fashion. In our present use case, the Meson interpreter converts Meson files to Ninja files that will be consumed by Ninja tool to effectively execute the building/deployment commands.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

## Design

A carefully crafted set of Meson files should be included in order to describe how to deploy the Nix repository, generating all the expected artifacts (command line tools, libraries, configuration files, documentation etc.)

This novel building infrastructure should be able to provide at least feature parity with the current quasi-autotools implementation, albeit in a different user interface.

# Examples and Interactions
[examples-and-interactions]: #examples-and-interactions

## Example interaction

Here is a table comparing some expected interactions:

| Action | Current | Meson+Ninja | Meson, backend-agnostic |
|-----------|--------------------------------|--------------------------------------|--------------------------------------|
| Configure | `./configure --enable-gc=true` | `meson setup build_dir -Dgc=enabled` | `meson setup build_dir -Dgc=enabled` |
| Build | `make` | `ninja -C build_dir build` | `meson -C build_dir compile` |
| Install | `make install` | `ninja -C build_dir install` | `meson -C build_dir install` |
| Uninstall | `make uninstall` | `ninja -C build_dir unistall` | `meson -C build_dir unistall` |
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

## Implementation

Currently, @p01arst0rm is working on an implementation from scratch.

# Drawbacks
[drawbacks]: #drawbacks

Below we will list some possible drawbacks and balances to them.

## Complexity and Bootstrap Issues

A new build system, and any build system for that matter, indirectly brings its own dependencies to the Nix project.

Ideally, such dependencies should be minimal, both in number and complexity, with extra good points for dependencies already present (e.g the ubiquitous C compiler).
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

About this specific point, a non-negligible drawback is: the reference implementation of Meson is written in Python. At least theoretically, it brings the necessity of including a Python 3 interpreter on the _bootstrap route_ of Nix.

However, some points can be laid out on the opposite side:

1. By design, the meson reference evaluator depends only on Python, avoiding the use of extra libraries.
2. Also by design, the implementation language is never exposed to the meson DSL. Because of it, the reimplementation of Meson in other programming languages becomes way more facilitated.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
1. Indeed, Muon is an alternative implementation of Meson written in C.
3. As part of this evaluation of this bootstrap route, we should also evaluate the current bootstrap route, in order to have a fair comparison.

Further, in principle the same criticisms and answers can be laid out for Ninja; however, Ninja is written in C++, a language already used to implement Nix. Therefore the bootstrap route suffers little to no alteration here.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

## Learning the new system

A somewhat subjective but non-negligible issue is the entry barrier of this new build system. Switching from a known build system to one unknown is not without its problems.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

However, the Meson development team strives to keep the DSL easy to learn and pleasurable to use. It should not be hard to become familiar with the Python-esque syntax of meson, and its functional, stateless approach is certainly a feature highly appreciated by the Nix community as a whole.

The huge advantages of implementing Meson surpass the small drawbacks of learning it.

## Source code changes

Further to the obvious inclusion of meson files (and the removal of the old quasi-autotools ones), there is a reasonable expectation of code refactoring.

However, such refactorings are completely validated on the long term goals of Nix, in particular the improvements on portability.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

## End users

The most known end user of Nix is certainly Nixpkgs. However, there are many other Linux distributions that already keep Nix on their repositories (15 families, according to Repology). There is also a reasonable expectation of affecting those package managers' devteams.

However, most (if not all) of those distributions already have Meson and its companion tool Ninja in their respective package databases (53 families, according to Repology), given that many open source projects use them as build system.
Comment on lines +133 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that be part of the “bootstrapping” subsection?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not so much. Bootstrapping is mostly useful for the Nix team.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't both paragraphs answering the question of “how hard it it to get Nix to build from scratch?”

Copy link
Member Author

@AndersonTorres AndersonTorres Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but from different perspectives.

For an already consolidated package manager like, say, Gentoo, they will merely put Meson and Ninja as build dependencies in their ebuilds.

For the "bootstrappers" with a C/C++ toolchain (already required for building Nix), they will need to compile Python from sources in order to compile Meson - or alternatively compile Muon.


## Transition between old and new build infrastructure

The transition between between the old and new build systems should be as smooth and controlled as possible.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

# Alternatives
[alternatives]: #alternatives
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of completeness there should probably also be an item here about bazel, buck & friends

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is well summarized on the examples of waf and premake.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly. The argument against them is that they are a language-specific DSL, but that's not the case of Bazel or Buck.

They do have a bootstrap problem ofc, but it's a different one (single implem, lots of deps)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A different one, and considerably worse than Meson. Cmake would be way better than them.

I do not believe they deserve more than a mention. If anyone is interested on picking Buck or Bazel or Imake, kudos to them!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well now, let's be fair, there's definitely more than one implementation of Bazel.

The original design of it is Google's internal closed-source thing designed specifically to meet Google's distributed cloud computing needs, after all. ;) Bazel itself is just an open-source attempt to make BUILD / *.bzl files look like an acceptable thing for other projects to include to make Google's life easier. ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My list of complaints:

  1. I know nothing about them

  2. They are built in Java

  3. There is no alternative implementation in another language like C or C++ or even Python

  4. I do not like Java

  5. Java support on Nixpkgs is not the best in this planet

  6. Bootstrap Java is a hell:

    Unfortunately, most of the software needed for the bootstrap has been abandoned. To ensure that the JDK can be built from sources without the need for an existing installation of the OpenJDK we propose to continue maintaining the links of this bootstrap chain.

  7. Java is heavier than Python

  8. Have I said I do not like Java?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep sounds about right 🤣

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe too late. Has Zig been considered? It claims being an alternative:

but you can use Zig as a replacement for autotools, cmake, make, scons, ninja, etc.

Zig is offering to replace a project’s build system with a reasonable language using a declarative API for building projects, that also provides package management, and thus the ability to actually depend on other C libraries. The ability to have dependencies enables higher level abstractions, and thus the proliferation of reusable high-level code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the entire point of meson is it offers the ability to compile all projects regardless of source language, build environment, linker, or compiler configurations. it offers flexibility and simplicity which allows easy maintainability into the future. the zig buildsystem is designed for c/c++ and is not nearly as well documented or flexible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also Zig is oriented to Zig itself, while Meson is general-purpose.
Using Zig as Autotools replacement would be as worse as - or even worser than - using Bazel or Waf.


The alternatives are

- Do nothing

It would keep the current code confusing and harder to work with, as stated on the motivation section.

- Use CMake

Indeed, CMake has many noteworthy advantages:

- Supports typical high level idiomatic constructions.
- Can generate GMake- and Ninja-compatible scripts.
- By design, Meson does not provide a Make backend.
- Both Meson and CMake support Microsoft Windows NT and Apple MacOS platforms
- As well as MSVC and XCode programming environments.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

- On the other hand, CMake DSL cmake language is arguably more complex and cumbersome.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved

- Evaluate other building systems (waf, premake, bazel, xmake etc.)

About this, a principle should be observed:

Per the bootstrap route issue discussed above, build tools strongly tied to other programming languages are strongly discouraged.

E.g. waf is basically a Python library, whereas premake and xmake are Lua libraries. They can't be decoupled of their implementation languages.

AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
- Use Bazel

For the sake of completeness, there is Bazel.

Advantages:

- Fast, scalable, multi-language and extensible.
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
- Backed by Google(TM).

Disadvantages:

- [Not fully open source yet](https://bazel.build/about/faq#are_you_done_open_sourcing_bazel)
- Written in Java
- Java bootstrap is fairly complex and completely dependent on _open-source abandonware_, as demonstrated by [Bootstrappable](https://bootstrappable.org/projects/java.html) project.
- At the time there is no alternative implementation of Bazel in another language.
- Backed by Google(TM).

# Unresolved questions
[unresolved]: #unresolved-questions

Questions that deserve further inquiry:

- Unexpected interactions with Meson and Ninja
- Specially, vendoring and reproducibility.

# Future work
[future]: #future-work

- Update project's continuous integration and related stuff;
- Deprecate and remove the current quasi-autotools scripts
- Preferably, the removal should be allocated to a minor version release.
- Evaluate the positive and negative impacts of such a change
AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
- Specially the most subjective ones.

AndersonTorres marked this conversation as resolved.
Show resolved Hide resolved
# References
[references]: #references

- [Meson](https://meson.build/) official site
- [Muon](https://muon.build/), a C99 alternative implementation
- [Ninja](https://ninja-build.org/) official site
- [Samurai](https://github.com/michaelforney/samurai), a C99 alternative implementation
- [Meson tutorial](https://mesonbuild.com/Porting-from-autotools.html) comparing autotools and Meson
- [NetBSD tutorial](https://wiki.netbsd.org/pkgsrc/how_to_convert_autotools_to_meson/) comparing Meson and autotools
- [Boostrappable Builds](https://bootstrappable.org/)
- [CMake](https://cmake.org/)
- [Xmake](https://xmake.io/)
- [Bazel](https://bazel.build)

- [Current work in progress from @p01arst0rm](https://github.com/NixOS/nix/pull/3160)