Skip to content

Commit 8f52e80

Browse files
committed
feat: add cocotb (Linux only)
Signed-off-by: Leo Moser <[email protected]>
1 parent 7e00bf5 commit 8f52e80

File tree

5 files changed

+125
-3
lines changed

5 files changed

+125
-3
lines changed

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ We compile and cache the tools for the following platforms:
2626
* (+ `klayout-gdsfactory` as a shorthand for an environment with both installed)
2727
* [Verilator](https://verilator.org)
2828
* [Icarus Verilog](https://github.com/steveicarus/iverilog)
29+
* [cocotb](https://www.cocotb.org/)
30+
* Linux only.
2931
* [Xschem](https://xschem.sourceforge.io/stefan/index.html)
3032
* [Xyce](https://github.com/xyce/xyce)
3133
* Linux only.

flake.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
self.composePythonOverlay (pkgs': pkgs: pypkgs': pypkgs: let
6363
callPythonPackage = lib.callPackageWith (pkgs' // pypkgs');
6464
in {
65+
cocotb = callPythonPackage ./nix/cocotb.nix {};
6566
kfactory = pypkgs.kfactory.overrideAttrs (attrs': attrs: {
6667
version = "1.9.3";
6768
src = pypkgs'.fetchPypi {
@@ -157,11 +158,12 @@
157158
yosys-slang
158159
]
159160
++ lib.optionals (lib.lists.any (el: el == system) yosys-ghdl.meta.platforms) [yosys-ghdl]);
160-
inherit (pkgs) magic magic-vlsi netgen klayout klayout-gdsfactory tclFull tk-x11 verilator xschem ngspice bitwuzla yosys yosys-sby yosys-eqy yosys-lighter yosys-slang;
161+
inherit (pkgs) magic magic-vlsi netgen klayout klayout-gdsfactory tclFull tk-x11 iverilog verilator xschem ngspice bitwuzla yosys yosys-sby yosys-eqy yosys-lighter yosys-slang;
161162
inherit (pkgs.python3.pkgs) gdsfactory gdstk tclint;
162163
}
163164
// lib.optionalAttrs self.legacyPackages."${system}".stdenv.hostPlatform.isLinux {
164165
inherit (pkgs) xyce;
166+
inherit (pkgs.python3.pkgs) cocotb;
165167
}
166168
// lib.optionalAttrs self.legacyPackages."${system}".stdenv.hostPlatform.isx86_64 {
167169
inherit (pkgs) yosys-ghdl;

nix/cocotb.nix

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Copyright 2025 nix-eda Contributors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# Code adapated from nixpkgs, original license follows
15+
# ---
16+
# Copyright (c) 2003-2025 Eelco Dolstra and the Nixpkgs/NixOS contributors
17+
#
18+
# Permission is hereby granted, free of charge, to any person obtaining
19+
# a copy of this software and associated documentation files (the
20+
# "Software"), to deal in the Software without restriction, including
21+
# without limitation the rights to use, copy, modify, merge, publish,
22+
# distribute, sublicense, and/or sell copies of the Software, and to
23+
# permit persons to whom the Software is furnished to do so, subject to
24+
# the following conditions:
25+
#
26+
# The above copyright notice and this permission notice shall be
27+
# included in all copies or substantial portions of the Software.
28+
#
29+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
{
37+
lib,
38+
buildPythonPackage,
39+
fetchFromGitHub,
40+
setuptools,
41+
setuptools-scm,
42+
cocotb-bus,
43+
find-libpython,
44+
pytestCheckHook,
45+
swig,
46+
iverilog,
47+
ghdl,
48+
stdenv,
49+
# Metadata
50+
version ? "1.9.2",
51+
sha256 ? "sha256-7KCo7g2I1rfm8QDHRm3ZKloHwjDIICnJCF8KhaFdvqY=",
52+
}:
53+
let
54+
self = buildPythonPackage rec {
55+
pname = "cocotb";
56+
inherit version;
57+
format = "setuptools";
58+
59+
# pypi source doesn't include tests
60+
src = fetchFromGitHub {
61+
owner = "cocotb";
62+
repo = "cocotb";
63+
tag = "v${version}";
64+
inherit sha256;
65+
};
66+
67+
nativeBuildInputs = [ setuptools-scm ];
68+
69+
buildInputs = [ setuptools ];
70+
propagatedBuildInputs = [ find-libpython ];
71+
72+
postPatch = ''
73+
patchShebangs bin/*.py
74+
75+
# POSIX portability (TODO: upstream this)
76+
for f in \
77+
cocotb/share/makefiles/Makefile.* \
78+
cocotb/share/makefiles/simulators/Makefile.*
79+
do
80+
substituteInPlace $f --replace 'shell which' 'shell command -v'
81+
done
82+
83+
# remove circular dependency cocotb-bus from setup.py
84+
substituteInPlace setup.py --replace "'cocotb-bus<1.0'" ""
85+
'';
86+
87+
disabledTests = [
88+
# https://github.com/cocotb/cocotb/commit/425e1edb8e7133f4a891f2f87552aa2748cd8d2c#diff-4df986cbc2b1a3f22172caea94f959d8fcb4a128105979e6e99c68139469960cL33
89+
"test_cocotb"
90+
"test_cocotb_parallel"
91+
];
92+
93+
nativeCheckInputs = [
94+
cocotb-bus
95+
pytestCheckHook
96+
swig
97+
iverilog
98+
ghdl
99+
];
100+
101+
preCheck = ''
102+
export PATH=$out/bin:$PATH
103+
mv cocotb cocotb.hidden
104+
'';
105+
106+
pythonImportsCheck = [ "cocotb" ];
107+
108+
meta = {
109+
changelog = "https://github.com/cocotb/cocotb/releases/tag/v${version}";
110+
description = "Coroutine based cosimulation library for writing VHDL and Verilog testbenches in Python";
111+
mainProgram = "cocotb-config";
112+
homepage = "https://github.com/cocotb/cocotb";
113+
license = lib.licenses.bsd3;
114+
broken = stdenv.hostPlatform.isDarwin;
115+
};
116+
};
117+
in
118+
self

nix/iverilog.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Ciel Contributors
1+
# Copyright 2025 nix-eda Contributors
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

nix/verilator.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Ciel Contributors
1+
# Copyright 2025 nix-eda Contributors
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)