Short Version

You can load a local default.nix or package.nix file taken from nixpkgs (or any nix derivation) with the following flake.nix:

{
  description = "A very basic flake that can load a default.nix from nixpkgs";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { self, nixpkgs }: let
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
  in {

    # Use callPackage to use the ./default.nix file
    packages.x86_64-linux.default = pkgs.callPackage ./default.nix {};

  };
}

Just change the callPackage call to the path of the default.nix and you are good to go.

Explanation

I wanted to play around with a default.nix from nixpkgs locally in a Nix flake. However, I could only find the following command, that can be used to load a local default.nix inside a nix-shell:

nix-shell -p '
let
  pkgs = import <nixpkgs> {};
in
pkgs.callPackage ./default.nix {}
'

However, I wanted to be able to load it as a Nix flake. And since I couldn’t find any instructions on how to that when I searched online, here’s a quick explanation of how to do it.

First, we need to initialize a flake:

nix flake init

This will create a basic flake.nix in your current directory:

{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { self, nixpkgs }: {

    packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;

    packages.x86_64-linux.default = self.packages.x86_64-linux.hello;

  };
}

To load a default.nix from nixpkgs, for example the default.nix of nmap, we need to change the outputs. We set packages.x86_64-linux.default to callPackage ./default.nix {}, which will load the specified ./default.nix:

{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { self, nixpkgs }: {

    # Use callPackage to use the ./default.nix file
    packages.x86_64-linux.default = nixpkgs.legacyPackages.x86_64-linux.callPackage ./default.nix {};

  };
}

For more information on callPackage see the NixOS manual or this nix.dev tutorial.

After this change, we can load the flake and work with nix shell and enjoy nmap:

$ nmap --help

Nmap 7.94 ( https://nmap.org )
Usage: nmap [Scan Type(s)] [Options] {target specification}
TARGET SPECIFICATION:
  Can pass hostnames, IP addresses, networks, etc.
  Ex: scanme.nmap.org, microsoft.com/24, 192.168.0.1; 10.0.0-255.1-254
  -iL <inputfilename>: Input from list of hosts/networks
  -iR <num hosts>: Choose random targets
  --exclude <host1[,host2][,host3],...>: Exclude hosts/networks
  --excludefile <exclude_file>: Exclude list from file
HOST DISCOVERY:
  -sL: List Scan - simply list targets to scan
  -sn: Ping Scan - disable port scan
...

To make it a bit easier to read, we can define the variable pkgs within a let expression and add a better description:

{
  description = "A very basic flake that can load a default.nix from nixpkgs";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { self, nixpkgs }: let
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
  in {

    # Use callPackage to use the ./default.nix file
    packages.x86_64-linux.default = pkgs.callPackage ./default.nix {};

  };
}