question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with Nix Flakes

See original GitHub issue

Hello!

I was just wondering how I would go about using this with Nix Flakes, as in what module I would have to import in nixpkgs.lib.nixosSystem.modules? Would mach-nix.nixosModules.mach-nix work?

Thank you kindly for the help!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:34 (31 by maintainers)

github_iconTop GitHub Comments

3reactions
nat543207commented, Oct 29, 2020

Flake inputs actually have some special handling that causes toString flake-input-name to return a Nix store path; as a result, you can import Nix files from flake inputs directly. I’ve been using that strategy in my own flake-based projects; a working (but very simple) example flake that does this with mach-nix is shown below:

{
  description = "Using mach-nix from a flake";

  inputs = {
    nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
    mach-nix.url = github:DavHau/mach-nix/f80ee22; # v3.0.0 release tag
  };

  outputs = { self, nixpkgs, mach-nix }: let
    pkgs = (import nixpkgs { system = "x86_64-linux"; }).pkgs;
    mach-nix-utils = import mach-nix {
      inherit pkgs;
      python = "python3";
    };
  in {

    packages.x86_64-linux.customPython = mach-nix-utils.mkPython {
      requirements = ''
        pytest
      '';
    };

    defaultPackage.x86_64-linux = self.packages.x86_64-linux.customPython;
  };
}

mkPython works just fine, which you can see for yourself by initializing a repo with the above flake and running

[nat543207@hostname|tmp] nix shell .#customPython                                                                                                                                                             
[nat543207@hostname|tmp] python
Python 3.8.6 (default, Sep 23 2020, 13:54:27)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytest
>>> pytest.fail('Intentional failure')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/6qbpa7k8mlgzb8gf67lms6ypx4q6vfp2-python3-3.8.6-env/lib/python3.8/site-packages/_pytest/outcomes.py", line 153, in fail
    raise Failed(msg=msg, pytrace=pytrace)
Failed: Intentional failure
>>>

However, functions like buildPythonApplication that rely on extractor don’t run correctly for some arguments (e. g. URLs) due to a <nixpkgs> import that can’t be evaluated in pure-mode by the flake. ~I’ve got a local patch to replace that import with the pkgs value used to initialize mach-nix; it hasn’t caused me any issues yet, so I’ll see if I can put a PR up for it tomorrow.~ This can be worked around by providing pname, version, and requirements manually in the argument attrset, which causes the logic that depends on the extractor to be skipped. My local patch doesn’t actually resolve this issue, and it looks like doing so would require digging deeper into the code than I have time for right now.

2reactions
DavHaucommented, Nov 25, 2020

I merged some commits of @nat543207 and @newkozlukov and added the library functions to flakes.nix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flakes - NixOS Wiki
Using flakes project from a legacy Nix ... There is a flake-compat library you can use to shim legacy default.nix and shell.nix files....
Read more >
Nix Flakes, Part 1: An introduction and tutorial - Tweag
Flakes are a solution to these problems. A flake is simply a source tree (such as a Git repository) containing a file named...
Read more >
Practical Nix Flakes - Serokell
nix flake set of subcommands is used to observe and manipulate flakes themselves rather than their outputs. nix flake show. This command takes...
Read more >
nix flake - NixOS
Flakes are the unit for packaging Nix code in a reproducible and discoverable way. They can have dependencies on other flakes, making it...
Read more >
nix-flakes.md - gists · GitHub
A flake is (usually) a Git repository that contains a file named flake. · Flakes provide an attribute set of values, such as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found